我在spring mvc中遇到错误我的代码如下
WEB-INF中的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com /xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>HelloWorld_MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>*.ds</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF中的helloWorld-servlet.xml文件
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- Default handler mapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- Map url request to Controller -->
<bean name="/hello.ds" class="controller.helloController">
</bean>
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
我的控制器类
package controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class helloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
System.out.println("Check");
// TODO Auto-generated method stub
String name=req.getParameter("name");
Map m=new HashMap();
m.put("msg","hello "+name);
ModelAndView mav=new ModelAndView("success",m );
return mav;
}
}
我的索引页
<form action="./hello.ds">
Name:<input type="text" name="name">
<br>
<input type="submit" value="Say">
</form>
错误堆栈如下:
INFO: No Spring WebApplicationInitializer types detected on classpath
Jan 02, 2017 5:04:50 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 02, 2017 5:04:50 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 02, 2017 5:04:50 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1216 ms
Jan 02, 2017 5:04:56 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'helloWorld'
Jan 02, 2017 5:04:56 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'helloWorld': initialization started
Jan 02, 2017 5:04:56 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'helloWorld- servlet': startup date [Mon Jan 02 17:04:56 IST 2017]; root of context hierarchy
Jan 02, 2017 5:04:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/helloWorld-servlet.xml]
Jan 02, 2017 5:04:56 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/hello.ds] onto handler '/hello.ds'
Jan 02, 2017 5:04:56 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanInitializationException: DispatcherServlet needs exactly 1 strategy for interface [org.springframework.web.servlet.FlashMapManager]
Jan 02, 2017 5:04:56 PM org.springframework.web.servlet.FrameworkServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanInitializationException: DispatcherServlet needs exactly 1 strategy for interface [org.springframework.web.servlet.FlashMapManager]
at org.springframework.web.servlet.DispatcherServlet.getDefaultStrategy(DispatcherServlet.java:797)
at org.springframework.web.servlet.DispatcherServlet.initFlashMapManager(DispatcherServlet.java:751)
at org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:492)
at org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:476)
at org.springframework.web.servlet.FrameworkServlet.onApplicationEvent(FrameworkServlet.java:806)
at org.springframework.web.servlet.FrameworkServlet$ContextRefreshListener.onApplicationEvent(FrameworkServlet.java:1121)
at org.springframework.web.servlet.FrameworkServlet$ContextRefreshListener.onApplicationEvent(FrameworkServlet.java:1117)
at org.springframework.context.event.GenericApplicationListenerAdapter.onApplicationEvent(GenericApplicationListenerAdapter.java:56)
at org.springframework.context.event.SourceFilteringListener.onApplicationEventInternal(SourceFilteringListener.java:107)
at org.springframework.context.event.SourceFilteringListener.onApplicationEvent(SourceFilteringListener.java:71)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
请告诉我这段代码有什么问题。 谢谢!!!
答案 0 :(得分:0)
异常&#39; BeanInitializationException:DispatcherServlet正好需要1个接口策略[org.springframework.web.servlet.FlashMapManager] &#39;将被抛出,因为DispatcherServlet正好需要1个接口策略,但这里获得的依赖性超过1个。
Class DispatcherServlet:http://javadox.com/org.springframework/spring-webmvc/4.0.6.RELEASE/org/springframework/web/servlet/DispatcherServlet.java.html
这意味着你为Spring MVC Dispatcher Servlet包含了多个jar。 尝试仅添加所需的jar并删除其他jar 。对于一个简单的Spring MVC程序,下面是必需的jar文件。