注解@RequestMapping 的使用


1首先@RequestMapping 中的值,我們說請求方法l路徑,請求url我們都知道怎么請求了,在第一節helloworld中,

我們先說我們先建一個類,RequestMappingTest 

方法如下:

/**
* 1. @RequestMapping 除了修飾方法, 還可來修飾類 2. 1). 類定義處: 提供初步的請求映射信息。相對於 WEB 應用的根目錄
* 2). 方法處: 提供進一步的細分映射信息。 相對於類定義處的 URL。若類定義處未標注 @RequestMapping,則方法處標記的 URL
* 相對於 WEB 應用的根目錄
*/

@RequestMapping("/requestMapping")
public String requestMapping(){
System.out.println("requestMapping()....");
return SUCCESS;

方法名字和@RequestMapping中的值一樣,這里面可以有兩種方式:

1當我們在類中RequestMappingTest 上可以加注解也可以不加注解@RequestMapping

當我們加注解的時候比如 @RequestMapping("/springMvc")

那么我們超鏈接的請求方式為   <a href="springMvc/requestMapping">springMvc/requestMapping</a><br/>

如果不加注解     <a href="requestMapping">requestMapping</a><br/>  

2.我們可以使用 method 屬性來指定請求方式

@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
public String testMethod() {
System.out.println("testMethod");
return SUCCESS;
}

那么我們超鏈接的請求方式為

<!--因為被注解是Post請求,本身請求是get,所以請求不到 -->
<a href="springMvc/testMethod">springMvc/testMethod</a><br/>

這樣是錯誤的,正確的方法是要post請求,那么我們可以建一個表單

<form action="springMvc/testMethod" method="post">
<input type="submit">

3.可以使用 params 和 headers 來更加精確的映射請求. params 和 headers 支持簡單的表達式

 

/**
*  可以使用 params 和 headers 來更加精確的映射請求. params 和 headers 支持簡單的表達式.
*
* @return
*/
@RequestMapping(value = "testParamsAndHeaders", params = { "username",
"age!=10" }, headers = { "Accept-Language=en-US,zh;q=0.8" })
public String testParamsAndHeaders() {
System.out.println("testParamsAndHeaders");
return SUCCESS;
}

那么我們超鏈接的請求方式為 

<a href="springmvc/testParamsAndHeaders?username=atguigu&age=10">Test ParamsAndHeaders</a>
<br><br>  ,headers中的Accept-Language=en-US,zh;q=0.8,我們可以用火狐瀏覽器按F12可以看到,這個是http協議中

的請求頭中的內容,我們也可以指定其他的,我們看下這個超鏈接肯定是錯誤的,因為我們方法中設置的是age!=10,而我們是age=10

所以,改過了就可以咯

 

4@PathVariable 可以來映射 URL 中的占位符到目標方法的參數中.

/**
* @PathVariable 可以來映射 URL 中的占位符到目標方法的參數中.
* @param id
* @return
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println("testPathVariable: " + id);
return SUCCESS;
}

我們超鏈接的請求方式為 :

<a href="springMvc/testPathVariable/101">springMvc/testPathVariable/101</a><br/>

 

5@RequestMapping 還 支持 Ant  格 URL 

@RequestMapping("/testAntPath/*/abc")
public String testAntPath() {
System.out.println("testAntPath");
return SUCCESS;
}

我們超鏈接的請求方式為 :

  <a href="springMvc/testAntPath/ddd/abc">springMvc/testAntPath</a><br/>,ddd的值可以是任意的

 

 6.我們來說下rest

 

我們可以先建一個jsp代碼如下:

<form action="springMvc/testRestPut/1" method="Post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="testRestPut">

</form><br>

<form action="springMvc/testRestDelete/1" method="Post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="testRestDelete">

</form><br>

<form action="springMvc/testRestPost/1" method="Post">
<input type="submit" value="testRestPost">

</form><br>

<a href="springMvc/testRestGet/1">testRestGet</a><br/>

我們用方法來測試一下:

@RequestMapping(value="/testRestPut/{id}",method=RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id){
System.out.println("testRestPut()..."+id);
return SUCCESS;
}
@RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id){
System.out.println("testRestDelete()..."+id);
return SUCCESS;
}
@RequestMapping(value="/testRestPost/{id}",method=RequestMethod.POST)
public String testRestPost(@PathVariable Integer id){
System.out.println("testRestPost()..."+id);
return SUCCESS;
}

@RequestMapping("/testRestGet/{id}")
public String testRestGet(@PathVariable Integer id){
System.out.println("testRestGet()..."+id);
return SUCCESS;
}

一一跟上面對應,我們來說明一下,我們知道本來就只有get,post請求,get,post請求我們就不說了

如何將post請求轉換成DELETE,和PUT呢,我們要建一個表單,這樣才能是post請求,

如果要轉換,那么我們要在表單中加入隱藏域,name屬性必須為 _method,value的值可以是

DELETE,PUT,為什么是這樣呢,我們要有個關鍵的細節沒做,做了在說;我們要在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>

,我們可以看下HiddenHttpMethodFilter 的源碼,Alt +Shirt+T 進入后你們看到有個常量是_method,

所以隱藏域中的name屬性必須是name屬性必須為 _method,value是post要轉換成的值,源碼有興趣的朋友

可以看下,value大小寫都沒關系,源碼中會全部轉換成大寫,

這樣我們用@PathVariable注解就可以傳入參數,這樣我們就可以進行CRUD了,以前我們學習

webservlet的時候 一般都是delete?id=1 這樣,現在不用這樣,這樣很方便,今天我們就講到這里。

希望看我博客的朋友繼續關注哦,我會把所有學習到的知識全部更在博客上。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM