今天试了下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代码

1. <servlet>  
2.   <servlet-name>spring</servlet-name>  
3.   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
4.   <init-param>  
5.      <param-name>contextConfigLocation</param-name>  
6.      <param-value>/WEB-INF/springContext.xml</param-value>  
7.   </init-param>  
8. </servlet>  
9.   
10. <servlet-mapping>  
11.   <servlet-name>spring</servlet-name>  
12.   <url-pattern>/*</url-pattern>  
13. </servlet-mapping>



 springContext.xml

Xml代码


1. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
2.     <property name="prefix" value="/WEB-INF/jsp/" />  
3.     <property name="suffix" value=".jsp" />  
4.  </bean>  
5.        
6.  <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"); }

   在项目/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代码


1. <servlet-mapping>  
2.     <servlet-name>spring</servlet-name>  
3. /*</url-pattern>  
4.   </servlet-mapping>


  改成

 

Java代码


1. <servlet-mapping>  
2.     <servlet-name>spring</servlet-name>  
3. /</url-pattern>  
4.   </servlet-mapping>

spring MVC的困惑--url-pattern的/和/*有区别

Spring MVC 遇到的一点点问题