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.xmlcss

Xml代碼 收藏代碼html

  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. <!-- Processes application requests -->
  6. <servlet>
  7. <servlet-name>appServlet</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <init-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:spring-servlet-context.xml</param-value>
  12. </init-param>
  13. <load-on-startup>0</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>appServlet</servlet-name>
  17. <url-pattern>/</url-pattern>
  18. </servlet-mapping>
  19. </web-app>

spring 的配置文件 spring-servlet-context.xmljava

Xml代碼 收藏代碼web

  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. <context:component-scan base-package="com.myPackage"/>
  9. <mvc:annotation-driven/>
  10. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  11. <property name="prefix" value="/WEB-INF/pages/" />
  12. <property name="suffix" value=".jsp" />
  13. </bean>
  14. </beans>

Java代碼 收藏代碼spring

  1. package com.myPackage.controller; 
  2. @Controller
  3. public class FooController{ 
  4. /**
  5. * 響應 /foo 請求,並返回 bar.jsp 作爲渲染頁面 (不必定非是jsp,只不過以上配置的是jsp)
  6. */
  7. @RequestMappting("/foo") 
  8. public String foo(){ 
  9. //process logic
  10. return "bar";  

以上即 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,圖片等),須要作以下配置spring-mvc

Xml代碼 收藏代碼mvc

  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.xmlapp

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. <context:component-scan base-package="com.myPackage"/>
  9. <mvc:resources mapping="/**" location="/" order="1"/>
  10. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  11. <property name="order" value="0"/>
  12. <property name="useDefaultSuffixPattern" value="false"/>
  13. </bean>
  14. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
  15. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16. <property name="prefix" value="/WEB-INF/pages/" />
  17. <property name="suffix" value=".jsp" />
  18. </bean>
  19. </beans>

或者這樣配置jsp

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. <context:component-scan base-package="com.myPackage"/>
  9. <mvc:resources mapping="/**/*.html" location="/" order="0"/>
  10. <mvc:resources mapping="/**" location="/" order="2"/>
  11. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  12. <property name="order" value="1"/>
  13. <property name="useDefaultSuffixPattern" value="true"/>
  14. </bean>
  15. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
  16. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  17. <property name="prefix" value="/WEB-INF/pages/" />
  18. <property name="suffix" value=".jsp" />
  19. </bean>
  20. </beans>
相關文章
相關標籤/搜索