SpringMVC的一個小整理
1.概述
Spring為展示層提供基於MVC設計理念的優秀web框架,是目前主流的MVC框架,而且從Spring3.0後全面超越了Strust2成為了最優秀的MVC框架,其通過一套註解,讓POJO成為處理請求的控制器,無需實現任何的接口,還支持REST風格的URL請求,比其他的MVC框架更具有擴展性和靈活性。
2.快速入門
(1)建立動態WEB 項目
(2)導入jar包
–commons-logging-1.1.3.jar
–spring-aop-4.0.0.RELEASE.jar
–spring-beans-4.0.0.RELEASE.jar
–spring-context-4.0.0.RELEASE.jar
–spring-core-4.0.0.RELEASE.jar
–spring-expression-4.0.0.RELEASE.jar
–spring-web-4.0.0.RELEASE.jar
–spring-webmvc-4.0.0.RELEASE.jar
(3)在web.xml文件中配置DispatcherServlet
默認是加載 /WEB- INF/<servletName-servlet>.xml 的 Spring 配置文件啟動 WEB 層 的 Spring 容器。可以通過 contextConfigLocation 初始化參數自定 義配置文件的位置和名稱
<!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> --> <!-- 大於等於0表示在服務器啟動的時候創建Servlet對象 小於0則是在第一次訪問的後手創建Servlet對象 --> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <!-- 攔截所有 --> <url-pattern>/</url-pattern> </servlet-mapping>
(4)配置自動掃描的包
(5)配置視圖解析器:視圖名稱解析器:將視圖邏輯名解析為: /WEB-INF/pages/<viewName>.jsp
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.neuedu.spring01.controller"></context:component-scan> <!-- InternalResourceViewResolver是視圖解析器,會將邏輯視圖轉化成時機的物理視圖 prefix+name+suffix --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
(6)編寫controller
(7)制作頁面
package com.neuedu.spring01.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class AController { private static final String SUCCESS="success"; private void println(Object str){ System.out.println(str); } /** * [email protected] * @return */ @RequestMapping(value="/sayHello",params={"!username"},headers="user-Agent") public String sayHello(){ return SUCCESS; } }
3 [email protected] 映射請求
Spring MVC 使用 @RequestMapping 註解為控制器指定可以處理哪些 URL 請求
1>在控制器的類定義及方法定義處都可標註
@RequestMapping
– 類定義處:提供初步的請求映射信息。相當於當前 WEB 應用的根目錄
– 方法處:提供進一步的細分映射信息。相對於類定義處的 URL。
若 類定義處未標註 @RequestMapping,則方法處標記的 URL 相當於當前 WEB 應用根目錄
若 類定義處標註 @RequestMapping,則方法處標記的 URL [email protected]!
@RequestMapping 除了可以使用請求 URL 映射請求外,還可以使用請求方法、請求參數及請求頭映射請求
@RequestMapping 的 value、method、params 及 heads 分別表示請求 URL、請求方法、請求參數及請求頭的映射條件,他們之間是與的關系,聯合使用多個條件可讓請求映射 更加精確化。
params 和 headers支持簡單的表達式:
– param1: 表示請求必須包含名為 param1 的請求參數
– !param1: 表示請求不能包含名為 param1 的請求參數
– param1 != value1: 表示請求包含名為 param1 的請求參數,但其值 不能為 value1
–{“param1=value1”, “param2”}: 請求必須包含名為 param1 和param2 的兩個請求參數,且 param1 參數的值必須為 value1!
Method:GET / POST /PUT /DELETE
@RequestMapping 還支持 Ant 風格的 URL:
–/user/*/createUser: 匹配
/user/aaa/createUser、/user/bbb/createUser 等 URL
–/user/**/createUser: 匹配
/user/createUser、/user/aaa/bbb/createUser 等 URL
–/user/createUser??: 匹配
/user/createUseraa、/user/createUserbb 等 URL
@PathVariable 映射 URL 綁定的占位符
帶占位符的 URL 是 Spring3.0 新增的功能,該功能在 SpringMVC 向 REST 目標挺進發展過程中具有裏程碑的意義
通過 @PathVariable 可以將 URL 中占位符參數綁定到控制器處理方法的入參中:URL 中的 {xxx} 占位符可以通過
@PathVariable("xxx") 綁定到操作方法的入參中,需要註意的是:該註解的value屬性值要與占位符保持一致。
4.REST:即 Representational State Transfer。(資源)表現層狀態轉化。是目前最流行的一種互聯網軟件架構。
它結構清晰、符合標準、易於理解、擴展方便, 所以正得到越來越多網站的采用.
HTTP 協議裏面,四個表示操作方式的動詞:GET、POST、PUT、DELETE。
它們分別對應四種基本操作:
GET 用來獲 取資源,
POST 用來新建資源,
PUT 用來更新資源,
DELETE 用來刪除資源。
示例:
–/order/1 HTTP GET :得到 id = 1 的 order
–/order/1 HTTP DELETE:刪除 id = 1的 order
–/order/1 HTTP PUT:更新id = 1的 order
–/order HTTP POST:新增 order
?HiddenHttpMethodFilter:瀏覽器 form 表單只支持 GET 與 POST 請求,而DELETE、PUT 等 method 並不支 持,Spring3.0 添加了一個過濾器,可以將這些請求轉換 為標準的 http 方法,使得支持 GET、POST、PUT 與 DELETE 請求。
5. POST請求如何轉化為put請求和delele請求?
1>在web.xml文件中配置:
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2>在表單域中需要攜帶一個name值為_method,value值為put或者delete的參數,如下所示:
<form action="${pageContext.request.contextPath}/order/1" method="post"> <input type="hidden" name="_method" value="put"/> <input type="submit" value="submitput"/> </form>
6 .針對POST請求亂碼的處理:
在web.xml文件中加上CharacterEncodingFilter
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
SpringMVC的一個小整理