spring mvc 基于注解 配置默认 handlermapping

spring mvc 是类似于 Struts 的框架。他们都有一个最主要的功能就是URL路由。URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 controller )映射起来。抛开其他的功能,spring mvc 要比 Struts 在URL路由功能上灵活很多。比如要实现 RESTful,如果用 Struts ,需要安装 一些插件,而且插件也常常限制的很死。但是如果用 spring mvc ,那就驾轻就熟。 

下面具体看一下spring mvc 
在 spring 2.5 以后,可以利用注解写进行路由映射,简单,直观。 
配置 web.xml 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  5.   
  6.     <!-- Processes application requests -->  
  7.     <servlet>  
  8.         <servlet-name>appServlet</servlet-name>  
  9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  10.         <init-param>  
  11.             <param-name>contextConfigLocation</param-name>  
  12.             <param-value>classpath:spring-servlet-context.xml</param-value>  
  13.         </init-param>  
  14.         <load-on-startup>0</load-on-startup>  
  15.     </servlet>  
  16.   
  17.     <servlet-mapping>  
  18.         <servlet-name>appServlet</servlet-name>  
  19.         <url-pattern>/</url-pattern>  
  20.     </servlet-mapping>  
  21. </web-app>  


spring 的配置文件 spring-servlet-context.xml 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"         
  5.        xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <context:component-scan base-package="com.myPackage"/>      
  10.   
  11. <mvc:annotation-driven/>  
  12.   
  13. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  14.        <property name="prefix" value="/WEB-INF/pages/" />                 
  15.        <property name="suffix" value=".jsp" />                
  16. </bean>  
  17.   
  18. </beans>  


Java代码  收藏代码
  1. package com.myPackage.controller;  
  2.   
  3. @Controller  
  4. public class FooController{  
  5.   
  6. /** 
  7. * 响应 /foo 请求,并返回 bar.jsp 做为渲染页面 (不一定非是jsp,只不过以上配置的是jsp) 
  8. */  
  9. @RequestMappting("/foo")  
  10. public String foo(){  
  11.     //process logic  
  12.     return "bar";   
  13. }  


以上即 spring mvc 一个简单的配置。mvc:annotation-driven 即告诉 spring mvc 使用注解的方式进行URL路由配置。controller 类上加上 @Controller 告诉 spring mvc 此类是 controller,同时方法上的 @RequestMapping 注解告诉 spring mvc 这里有一个请求映射到这个方法。 

前面都是最基本的配置,但有一个缺点就是还不能响应静态页面的请求。 比如 webroot 下有 一个 bar.html 页面,但 webroot/bar.html 是请求不到 bar.html 的。因为目前为止,还没有对 bar.html 的路由映射。 

要能请求静态页面,或者资源(如css,js,图片等),需要做如下配置 
Xml代码  收藏代码
  1. <mvc:resources mapping="/**" location="/" />  


但是上面的配置还是有问题的,如果有一个 foo.html 的页面,请求是 webroot/foo.html,但是会发现会被路由到 /foo.* ,然后程序交给了 foo() 方法,没能得到 foo.html 页面。原因是使用 mvc:annotation-driven 做配置,spring mvc 会默认使用 DefaultAnnoationHandlerMapping 和 AnnotationMethodHandlerAdapter ,而 DefaultAnnotationHandlerMapping 会默认对 @RequestMapping 上的路由做三个映射,如 @RequestMapping("/foo") 会被做 /foo 、/foo.* 和 /foo/ 这样三个映射。mvc:resources 默认使用 SimpleUrlHandlerMapping, 做映射,但由于 SimpleUrlHandlerMapping 的优先级(order)没有 DefaultAnnotationHandlerMapping 高。 所以本来应该是 foo.html 静态资源响应,被映射为了 /foo.* 。@RequestMapping 的三次映射可以通过 DefaultAnnotationHandlerMapping#setUseDefaultSuffixPattern(false) 取消。XxxxHandlerMapping 可以通过 order 属性改变。 

所以总结以上设置,可以这样正确的配置 spring-servlet-context.xml 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"         
  5.        xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <context:component-scan base-package="com.myPackage"/>      
  10.   
  11. <mvc:resources mapping="/**" location="/" order="1"/>  
  12.   
  13. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
  14.         <property name="order" value="0"/>  
  15.         <property name="useDefaultSuffixPattern" value="false"/>  
  16.     </bean>  
  17.   
  18. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  19.   
  20. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.        <property name="prefix" value="/WEB-INF/pages/" />                 
  22.        <property name="suffix" value=".jsp" />                
  23. </bean>  
  24. </beans>  


或者这样配置 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"         
  5.        xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <context:component-scan base-package="com.myPackage"/>      
  10.   
  11. <mvc:resources mapping="/**/*.html" location="/" order="0"/>  
  12. <mvc:resources mapping="/**" location="/" order="2"/>  
  13.   
  14. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
  15.         <property name="order" value="1"/>  
  16.         <property name="useDefaultSuffixPattern" value="true"/>  
  17.     </bean>  
  18.   
  19. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  20.   
  21. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  22.        <property name="prefix" value="/WEB-INF/pages/" />                 
  23.        <property name="suffix" value=".jsp" />                
  24. </bean>  
  25. </beans>  
原文地址:https://www.cnblogs.com/chenying99/p/2381232.html