@RequestParam,你一定見過;@PathVariable,你肯定也知道;@QueryParam,你怎么會不曉得?!還有你熟悉的他(@CookieValue)!她(@ModelAndView)!它(@ModelAttribute)!沒錯,僅注解這塊,spring mvc就為你打開了五彩斑斕的世界。來來來,不要興(mi)奮(hu),坐下來,我們好好聊聊這么些個注解兄弟們~~~(wait, 都沒有聽過? 好,來,你坐前排,就你!)
一、spring mvc如何匹配請求路徑——“請求路徑哪家強,RequestMapping名遠揚”
@RequestMapping是用來映射請求的,比如get請求,post請求,或者REST風格與非REST風格的。 該注解可以用在類上或者方法上,如果用于類上,表示該類中所有方法的父路徑。
舉例(這里用到的測試類如SpringMVCTest以及一些頁面在第一篇《學習SpringMVC——從HelloWorld開始》中已經介紹):
SpringMVCTest.java中加入測試方法:
1
2
3
4
5
6
7
8
9
|
@RequestMapping ( "/testRequestMapping" ) public String testRequestMapping(){ System.out.println( "testRequestMapping" ); return SUCCESS; } |
注意這里在方法級別上添加了注解@RequestMapping(“/testRequestMapping”), 表示可以通過“/testRequestMapping”相對路徑來定位到這個方法,同時我們在SpringMVCTest類上也放了一個類級別的RequestMapping的注解:
1
2
3
4
5
|
@RequestMapping ( "/springmvc" ) @Controller public class SpringMVCTest { |
注意這里還添加了一個@Controller的注解,該注解在SpringMVC 中,負責處理由DispatcherServlet 分發的請求,它把用戶請求的數據經過業務處理層處理之后封裝成一個Model ,然后再把該Model 返回給對應的View 進行展示。至此有了一個“springmvc/testRequestMapping”這樣的路徑,我們就能夠定位到testRequestMapping這個方法上,然后執行方法內的方法體。
再補充一點,RequestMapping可以實現模糊匹配路徑,比如:
?:匹配一個字符
*:匹配任意字符
**:匹配多層路徑
/springmvc/**/lastTest 就可以匹配/springmvc/firstTest/secondTest/lastTest這樣的路徑
二、spring mvc如何獲取請求的參數——“八仙過海,各顯神通”
1. @PathVariable
該注解用來映射請求URL中綁定的占位符。通過@PathVariable可以將URL中占位符的參數綁定到controller處理方法的入參中,沒聽懂?看例子:
1
2
3
4
5
6
7
8
9
|
@RequestMapping ( "/testPathVariable/{id}" ) public String testPathVariable( @PathVariable (value= "id" ) Integer id){ System.out.println( "testPathVariable:" + id); return SUCCESS; } |
在index.jsp中我們添加一條連接,用來觸發一個請求:
1
|
<a href= "springmvc/testPathVariable/1" >testPathVariable</a><br/><br/> |
我們可以看到這里有一個超鏈接,點擊后會進入到springmvc/testPathVariable/1對應的controller處理的方法中,那我們現在就是想獲取到這個請求參數中的“1”,所以在testPathVariable方法上加入“/testPathVariable/id”,關于 id”,關于{id}的具體對應在該方法的參數中,通過@PathVariable(value="id")來聲明要接收的請求參數,并通過Integer id來綁定和接收。通過該種方式,我們就可以得到前臺頁面請求的參數“1”。
2. @RequestParam
該注解也是用來獲取請求參數的。那么該注解和@PathVariable有何不同呢? 還是看例子:
在SpringMVCTest中添加方法
1
2
3
4
5
6
7
8
9
|
@RequestMapping (value= "/testRequestParam" ) public String testRequestParam( @RequestParam (value= "username" ) String username, @RequestParam (value= "age" , required= false , defaultValue= "0" ) int age){ System.out.println( "testRequestParam" + " username:" + username + " age:" +age); return SUCCESS; } |
在index.jsp添加超鏈接標簽
1
|
<a href= "springmvc/testRequestParam?username=jackie&age=12" >testRequestParam</a><br/><br/> |
點擊頁面上的超鏈接,就會匹配controller中testRequestParam方法上的RequestMapping的路徑。注意在該方法中,我們通過@RequestParam這個注解聲明了兩個變量,用來獲取請求中query所帶的參數值,一個是username后的值,另一個是age后面的值。
看到這里,你大概已經明白了@PathVariable和@RequestParam之間的一些區別了吧,對于像“springmvc/testPathVariable/1”這樣的請求,我們通過@PathVariable來綁定請求的參數;而對于類似“springmvc/testRequestParam?username=jackie&age=12”這樣的請求參數是以鍵值對出現的,我們通過@RequestParam來獲取到如username或age后的具體請求值。
與RequestParam有異曲同工用法的還有QueryParam,因其不是spring mvc框架內的注解,這里不再詳述。
對于不同的請求類型和請求方式,spring mvc都有一套針對的解決方案,下面我們來看看當下比較流行的REST風格的請求是啥樣的——利用REST風格實現增刪改查。
在SpringMVCTest類中自下而上的實現了查(get)增(post)刪(delete)和改(put)的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
@RequestMapping (value= "/testRest/{id}" , method=RequestMethod.PUT) public String testRestPut( @PathVariable (value= "id" ) Integer id){ System.out.println( "test put:" + id); return SUCCESS; } @RequestMapping (value= "/testRest/{id}" , method=RequestMethod.DELETE) public String testRestDelete( @PathVariable (value= "id" ) Integer id){ System.out.println( "test delete:" + id); return SUCCESS; } @RequestMapping (value= "/testRest" , method=RequestMethod.POST) public String testRest(){ System.out.println( "test post" ); return SUCCESS; } @RequestMapping (value= "/testRest/{id}" , method=RequestMethod.GET) public String testRest( @PathVariable (value= "id" ) Integer id){ System.out.println( "test get:" + id); return SUCCESS; } |
那么前臺界面如何實現呢,相對應的順序為
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<form action= "springmvc/testRest/1" method= "post" > <input type= "hidden" name= "_method" value= "PUT" /> <input type= "submit" value= "testRestPut" /> </form><br/><br/> <form action= "springmvc/testRest/1" method= "post" > <input type= "hidden" name= "_method" value= "DELETE" /> <input type= "submit" value= "TestRest DELETE" /> </form><br><br> <form action= "springmvc/testRest" method= "post" > <input type= "submit" value= "testRestPost" > </form><br/><br/> <a href= "springmvc/testRest/1" >testRest</a><br/><br/> |
除此之外,我們還需要在配置文件web.xml中添加支持將post轉化為delete和put請求的聲明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!-- 配置HiddenHttpMethodFilter:可以把POST請求轉為DELETE或POST請求 --> < 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 > |
如你所見,這里的改和刪都是通過post的方式發送出去的,因為這里不支持put和delete來直接實現刪改,而是通過借助post方式,并悄悄的帶上一塊令牌hidden類型的input標簽來告訴后臺我在前臺發送的實際上是刪和改的請求。
那么這個過程時如何實現的呢,為什么加上
1
|
<input type= "hidden" name= "_method" value= "DELETE" /> |
這塊令牌,人家后臺就要買你的賬呢。那我們就來看看后來是如何買賬的吧。
歸根到底還是得益于添加在web.xml中的HiddenHttpMethodFilter這個類,在該類中有一個方法doFilterInternal, 通過調試我們可以發現其中端倪,啟動tomcat(不能是tomcat8),點擊delete操作對應的input標簽,進入調試界面,我們可以看到:
- 通過request.getParameter(this.methodParam)在request域中得到this.methodParam(_method)的值,對應于刪除delete的操作,在頁面上,delete中聲明了一個hidden的input,其中name就是“_method”,value就是DELETE,所以這里得到的paramValue的值為“DELETE”
- 繼續執行,可以看到通過request.getMethod的取值是否與“POST”相等,顯然,這里是相等,因為我們在前臺頁面中對于delete的操作請求中method聲明為post方式
- 再往后就是將獲取到的請求方法封裝HttpServletRequest中,完成后續的處理。這里我們應該明白了為什么前臺要加上那樣一個hidden的input了。
小坑:這里注意啟動不能是tomcat8,而只能是比8小的版本,如7或6等,下圖展示了用tomcat的報錯信息和用7的成功響應:
總結下,如何發送put和delete的請求:
1.在web.xml中配置HiddenHttpMethodFilter
2.發送post請求
3.請求中是個隱藏域,name為”_mothod”,value為put或delete
最后再來說下@CookieValue這個注解。
3. @CookieValue
該注解也是差不多的套路,也是一種映射,映射的是一個Cookie值。
在我們發送一個請求時,我們可以看到請求中攜帶了一些cookie值
比如這里的JSESSIONID或者Path等。現在我們就寫個方法用于獲取Cookie值。
在SpringMVCTest中添加
1
2
3
4
5
6
7
8
9
|
@RequestMapping (value= "/testCookieValue" ) public String testCookieValue( @CookieValue ( "JSESSIONID" ) String cookieValue){ System.out.println( "testCookieValue: " + cookieValue); return SUCCESS; } |
index.jsp界面上添加鏈接
1
|
<a href= "springmvc/testCookieValue" >testCookieValue</a><br/><br/> |
這樣我們就可以得到類似“testCookieValue: 1410F05C9ADD84E8659C2AC79E8CC666”這樣的結果。
至此,我們介紹了
1.@RequestMapping的用法
2.獲取請求參數的@PathVariable、@RequestParam的用法
3.介紹如何實現REST風格的請求,并分析了post如何轉化為delete和put請求
4.介紹了@CookieValue的用法
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/bigdataZJ/p/springmvc2.html