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
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- Processes application requests -->
- <servlet>
- <servlet-name>appServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-servlet-context.xml</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>appServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
spring 的配置文件 spring-servlet-context.xmljava
Xml代碼
web
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <context:component-scan base-package="com.myPackage"/>
- <mvc:annotation-driven/>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/pages/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
Java代碼
spring
- package com.myPackage.controller;
- @Controller
- public class FooController{
- /**
- * 響應 /foo 請求,並返回 bar.jsp 作爲渲染頁面 (不必定非是jsp,只不過以上配置的是jsp)
- */
- @RequestMappting("/foo")
- public String foo(){
- //process logic
- 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
- <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代碼
框架
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <context:component-scan base-package="com.myPackage"/>
- <mvc:resources mapping="/**" location="/" order="1"/>
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
- <property name="order" value="0"/>
- <property name="useDefaultSuffixPattern" value="false"/>
- </bean>
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/pages/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
或者這樣配置jsp
Xml代碼 ![收藏代碼](http://static.javashuo.com/static/loading.gif)
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <context:component-scan base-package="com.myPackage"/>
- <mvc:resources mapping="/**/*.html" location="/" order="0"/>
- <mvc:resources mapping="/**" location="/" order="2"/>
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
- <property name="order" value="1"/>
- <property name="useDefaultSuffixPattern" value="true"/>
- </bean>
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/pages/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>