今天试了下spring mvc遇到个很奇怪的事情,按照书上配置访问时总是出现如下警告:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/myspring/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'spring'
web.xml
Xml代码
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/springContext.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
springContext.xml
Xml代码
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <bean name="/hc" class="com.hj.controllers.HelloController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean name="/hc" class="com.hj.controllers.HelloController"/>
HelloController.java
Java代码
- @Override
- protected ModelAndView handleRequestInternal(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- return new ModelAndView("hello");
- }
@Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("hello"); }
在项目/WEB-INF/jsp目录下防止hello.jsp文件。按照上述配置访问http://localhost:8081/myspring /hc,应该是出现hello.jsp内容才对,但是却出现了一开始提到的警告。我搜索了许多地方,结果在一老外的提供中找到了同样的问题(http: //forum.springsource.org/archive/index.php/t-71263.html),解决方法老外也说明了,只是不知 道为什么。
就是将web.xml中
Xml代码
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
改成
Java代码
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>