欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC?@RequestMapping注解詳解

 更新時(shí)間:2021年12月31日 09:44:36   作者:智商三歲半i  
本文主要介紹了SpringMVC?@RequestMapping注解詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、@RequestMapping

@RequestMapping注解的源碼:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";
    @AliasFor("path")
    String[] value() default {};
    @AliasFor("value")
    String[] path() default {};
    RequestMethod[] method() default {};
    String[] params() default {};
    String[] headers() default {};
    String[] consumes() default {};
    String[] produces() default {};
}

1.@RequestMapping注解的功能

@RequestMapping的功能就是將請(qǐng)求和處理請(qǐng)求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系。
SpringMVC接收到指定的請(qǐng)求,就會(huì)來找到在映射關(guān)系中對(duì)應(yīng)的控制器方法來處理這個(gè)請(qǐng)求。

2.@RequestMapping注解的位置

@RequestMapping標(biāo)注在類:設(shè)置映射請(qǐng)求請(qǐng)求路徑的初始信息(通常用于表示某個(gè)模塊的信息)
@RequestMapping標(biāo)注在方法:設(shè)置映射請(qǐng)求請(qǐng)求路徑的具體信息
@RequestMapping相同的請(qǐng)求地址一定只有一個(gè)RequestMapping映射,否則會(huì)報(bào)錯(cuò)。
瀏覽器先匹配類上標(biāo)注的@Requestmapping,再匹配方法上的@Requestmapping,即請(qǐng)求地址url = 類請(qǐng)求路徑 + 方法請(qǐng)求路徑

@Controller
@RequestMapping("/hello")
public class DisplayController {
    @RequestMapping("/displayTest")
    //此時(shí)的請(qǐng)求實(shí)際就是/hello/displayTest
    public String displayTest(){
        return "display";
    }
}

此時(shí)需要映射的請(qǐng)求地址實(shí)際就是http://localhost:8080/hello/display

<!--瀏覽器解析的是絕對(duì)路徑,會(huì)將"/"解析為"localhost:8080/"-->
<a th:href="@{/hello/displayTest}" rel="external nofollow" >訪問display頁面...</a>

二、@RequestMapping注解的屬性

1.value屬性(掌握)

value屬性是通過請(qǐng)求地址來匹配請(qǐng)求映射(默認(rèn)是value),value屬性是一個(gè)String類型的數(shù)組,表示該請(qǐng)求能夠匹配多個(gè)請(qǐng)求地址所對(duì)應(yīng)的請(qǐng)求,value屬性必須設(shè)設(shè)置,至少通過請(qǐng)求地址映射來匹配請(qǐng)求映射。

//表示可以通過地址testWorld或者h(yuǎn)ello/testWorld都可以訪問world.html
//@RequestMapping請(qǐng)求地址與方法名沒有關(guān)系,但是為了方便習(xí)慣上使用方法名
@RequestMapping(value = {"testWorld","hello/testWorld"})
public String testWorld(){
    return "world";
}

此時(shí)需要映射的請(qǐng)求地址實(shí)際就是http://localhost:8080/testWorld或者http://localhost:8080/hello/testWorld

<!--瀏覽器解析的是絕對(duì)路徑,會(huì)將"/"解析為"localhost:8080/"-->
<a th:href="@{/hello/testWorld}" rel="external nofollow" >@{/hello/testWorld}訪問helloworld頁面...</a><br>
<a th:href="@{/testWorld}" rel="external nofollow"  rel="external nofollow" >@{/testWorld}訪問helloworld頁面...</a><br>

2.method屬性(掌握)

method是通過請(qǐng)求方式(get/post)匹配請(qǐng)求映射,method屬性是一個(gè)RequestMethod[]類型的數(shù)組,表示該種請(qǐng)求可以匹配多種請(qǐng)求方式對(duì)應(yīng)的請(qǐng)求。如果當(dāng)前請(qǐng)求的請(qǐng)求映射滿足value屬性,但是不滿足method屬性,就會(huì)報(bào)錯(cuò)405,報(bào)錯(cuò):Request method 'POST' not supported

get和post兩種常見的請(qǐng)求方式有何區(qū)別?
get:使用?將請(qǐng)求參數(shù)與請(qǐng)求地址拼接起來,格式:?請(qǐng)求參數(shù)名=請(qǐng)求參數(shù)值&請(qǐng)求參數(shù)名=請(qǐng)求參數(shù)值,不安全,傳輸速度快(請(qǐng)求地址也會(huì)傳輸),傳輸數(shù)據(jù)量有限,不能用于上傳文件
post:請(qǐng)求參數(shù)放在請(qǐng)求體中(格式:請(qǐng)求參數(shù)名=請(qǐng)求參數(shù)值&請(qǐng)求參數(shù)名=請(qǐng)求參數(shù)值),安全,傳輸速度慢,傳輸數(shù)據(jù)量大

//表示可以通過地址testWorld或者h(yuǎn)ello/testWorld都可以訪問world.html
@RequestMapping(value = {"testWorld","hello/testWorld"},method = {RequestMethod.GET,RequestMethod.POST})
public String testWorld(){
    return "world";
}
<!--瀏覽器解析的是絕對(duì)路徑,會(huì)將"/"解析為"localhost:8080/"-->
<a th:href="@{/testWorld}" rel="external nofollow"  rel="external nofollow" >@{/testWorld}訪問helloworld頁面(GET請(qǐng)求(默認(rèn)))</a><br>
<form th:action="@{/testWorld}" method="post">
    <input type="submit" value="@{/testWorld}訪問helloworld頁面(POST請(qǐng)求)">
</form>

此時(shí)訪問get請(qǐng)求正常,訪問post請(qǐng)求,請(qǐng)求方式不匹配(請(qǐng)求不支持),報(bào)錯(cuò)誤405

在這里插入圖片描述

將WorldController的@RequestMapping改為@RequestMapping(value = {"testWorld","hello/testWorld"},method = {RequestMethod.GET,RequestMethod.POST}),再次訪問post和get請(qǐng)求,請(qǐng)求成功。

在這里插入圖片描述

3.params屬性(了解)

params屬性是指通過請(qǐng)求的參數(shù)匹配處理請(qǐng)求的映射,params屬性是一個(gè)String類型的數(shù)組,表示通過四種表達(dá)式設(shè)置請(qǐng)求參數(shù)和請(qǐng)求映射匹配關(guān)系。
“param”:要求請(qǐng)求映射所匹配的請(qǐng)求必須攜帶param請(qǐng)求參數(shù)
“!param”:要求請(qǐng)求映射所匹配的請(qǐng)求必須不能攜帶param請(qǐng)求參數(shù)
“param=value”:要求請(qǐng)求映射所匹配的請(qǐng)求必須攜帶param請(qǐng)求參數(shù)且param=value
“param!=value”:要求請(qǐng)求映射所匹配的請(qǐng)求必須攜帶param請(qǐng)求參數(shù)但是param!=value

//處理請(qǐng)求的參數(shù)只能是username和password,且username=admin、password=12345678
@RequestMapping(value = "/testParams",params = {"username=admin","password=12345678"})
public String testParams(){
    return "world";
}
<!--瀏覽器解析的是絕對(duì)路徑,會(huì)將"/"解析為"localhost:8080/"-->
<!--此處使用單引號(hào),雙引號(hào)會(huì)報(bào)錯(cuò)-->
<!--會(huì)自動(dòng)轉(zhuǎn)化為?傳參-->
<a th:href="@{/testParams(username='admin',password=12345678)}" rel="external nofollow"  rel="external nofollow" >測試params參數(shù)</a><br>

可以看到請(qǐng)求參數(shù)已經(jīng)使用?進(jìn)行了拼接,請(qǐng)求訪問成功,

在這里插入圖片描述

將請(qǐng)求改為如下,再次訪問,@RequestMapping請(qǐng)求參數(shù)不匹配,報(bào)400

<a th:href="@{/testParams(username='root',password=12345678)}" rel="external nofollow" >測試params參數(shù)root</a><br>

在這里插入圖片描述

4.headers屬性(了解)

headers屬性通過請(qǐng)求的請(qǐng)求頭信息匹配請(qǐng)求映射,headers屬性是一個(gè)String類型的數(shù)組,表示可以通過四種表達(dá)式設(shè)置請(qǐng)求頭信息和請(qǐng)求映射的匹配信息。
“header”:要求請(qǐng)求映射所匹配的請(qǐng)求必須攜帶header請(qǐng)求頭信息
“!header”:要求請(qǐng)求映射所匹配的請(qǐng)求必須不能攜帶header請(qǐng)求頭信息
“header=value”:要求請(qǐng)求映射所匹配的請(qǐng)求必須攜帶header請(qǐng)求頭信息且header=value
“header!=value”:要求請(qǐng)求映射所匹配的請(qǐng)求必須攜帶header請(qǐng)求頭信息且header!=value
若當(dāng)前請(qǐng)求滿足@RequestMapping注解的value和method屬性,但是不滿足headers屬性,此時(shí)頁面顯示404錯(cuò)誤,即資源未找到

//此時(shí)的請(qǐng)求的params中username=admin,password=1234545678,并且端口號(hào)是8081才可以成功,否則報(bào)錯(cuò)
@RequestMapping(value = "/testParams",
        params = {"username=admin","password=12345678"},
        headers = {"host=localhost:8081"})
public String testParams(){
    return "world";
}
<a th:href="@{/testParams(username='admin',password=12345678)}" rel="external nofollow"  rel="external nofollow" >測試params參數(shù)</a><br>

可以看到未找到,@RequestMapping的請(qǐng)求頭參數(shù)不匹配,報(bào)錯(cuò)誤404

在這里插入圖片描述

將端口號(hào)改為8080,再次測試。

//此時(shí)的請(qǐng)求的params中username=admin,password=1234545678,并且端口號(hào)是8080才可以成功,否則報(bào)錯(cuò)
@RequestMapping(value = "/testParams",
        params = {"username=admin","password=12345678"},
        headers = {"host=localhost:8080"})
public String testParams(){
    return "world";
}

可以看到訪問成功

在這里插入圖片描述

總結(jié):
@RequestMapping沒有和任意參數(shù)匹配,報(bào)錯(cuò)誤404
@RequestMapping的請(qǐng)求頭參數(shù)不匹配,報(bào)錯(cuò)誤404
@RequestMapping請(qǐng)求參數(shù)(params)不匹配,報(bào)錯(cuò)誤400
@RequestMapping請(qǐng)求方式不匹配(請(qǐng)求不支持),報(bào)錯(cuò)誤405

5.SpringMVC支持ant風(fēng)格的路徑

?:表示任意的單個(gè)字符
*:表示任意的0個(gè)或多個(gè)字符
**:表示任意的一層或多層目錄
注意:在使用**時(shí),只能使用/**/xxx的方式

//@RequestMapping(value = "/a?a/testAnt")
//@RequestMapping(value = "/a*a/testAnt")
@RequestMapping(value = "/**/testAnt")
public String testAnt(){
    return "world";
}
<a th:href="@{/a1a/testAnt}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >測試ant風(fēng)格--->?</a><br>
<a th:href="@{/a1a/testAnt}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >測試ant風(fēng)格--->*</a><br>
<a th:href="@{/a1a/testAnt}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >測試ant風(fēng)格--->**</a><br>

6.SpringMVC支持路徑中的占位符(重點(diǎn))

原始方式:/deleteUser?id=1
rest方式::/deleteUser/1
SpringMVC路徑中的占位符常用于restful風(fēng)格中,當(dāng)請(qǐng)求路徑中將某些數(shù)據(jù)通過路徑的方式傳輸?shù)椒?wù)器中,就可以在相應(yīng)的@RequestMapping注解的value屬性中通過占位符{xxx}表示傳輸?shù)臄?shù)據(jù),在通過@PathVariable注解,將占位符所表示的數(shù)據(jù)賦值給控制器方法的形參。

@RequestMapping("/testRest/{username}/{password}")
public String testRest(@PathVariable("username") String username,@PathVariable("password") String password){
    System.out.println("username="+username);
    System.out.println("password="+password);
    return "world";
}
<!--瀏覽器解析的是絕對(duì)路徑,會(huì)將"/"解析為"localhost:8080/"-->
<a th:href="@{/testRest/小王/123456}" rel="external nofollow" >測試rest占位符</a><br>

可以看到?jīng)]有使用?連接,使用了rest方式實(shí)現(xiàn)了訪問。

在這里插入圖片描述

查看idea的控制臺(tái),數(shù)據(jù)通過路徑的方式傳輸?shù)椒?wù)器中。

在這里插入圖片描述

三、@RequestMapping的派生類注解

對(duì)于處理指定請(qǐng)求方式的控制器方式,SpringMVC提供了@RequestMapping的派生類注解:
處理post請(qǐng)求的映射—>@PostMapping
處理get請(qǐng)求的映射—>@GetMapping
處理put請(qǐng)求的映射—>@PutMapping
處理delete請(qǐng)求的映射—>@DeleteMapping
但是目前瀏覽器只支持get和post,若在form表單提交時(shí),為method設(shè)置了其他請(qǐng)求方式的字符串(put或delete),則按照默認(rèn)的請(qǐng)求方式(get)處理。

@GetMapping("testGetMapping")
public String testGetMapping(){
    return "world";
}
<!--瀏覽器解析的是絕對(duì)路徑,會(huì)將"/"解析為"localhost:8080/"-->
<a th:href="@{/testGetMapping}" rel="external nofollow" >@{/testGetMapping}訪問helloworld頁面...</a><br>

在這里插入圖片描述

測試form表單是否支持put或delete方式的請(qǐng)求

當(dāng)請(qǐng)求和請(qǐng)求映射的方式不匹配時(shí)就會(huì)報(bào)錯(cuò)405.

//當(dāng)前請(qǐng)求映射的請(qǐng)求地址是testPut,請(qǐng)求方式是put方式
@RequestMapping(value = "/testPut",method = RequestMethod.PUT)
public String testPut(){
    return "world";
}
<!--測試form表單是否支持put或delete方式的請(qǐng)求-->
<form th:action="@{/testPut}" method="put">
    <input type="submit" value="測試form表單是否支持put或delete方式的請(qǐng)求">
</form>

結(jié)果如下:
若在form表單提交時(shí),為method設(shè)置了其他請(qǐng)求方式的字符串(put或delete),則按照默認(rèn)的請(qǐng)求方式(get)處理。若要發(fā)送put和delete請(qǐng)求,則需要通過spring提供的過濾器HiddenHttpMethodFilter,在restful部分會(huì)講到。

在這里插入圖片描述

到此這篇關(guān)于SpringMVC @RequestMapping注解詳解的文章就介紹到這了,更多相關(guān)SpringMVC @RequestMapping內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Servlet線程中AsyncContext異步處理Http請(qǐng)求

    Java Servlet線程中AsyncContext異步處理Http請(qǐng)求

    這篇文章主要介紹了Java Servlet線程中AsyncContext異步處理Http請(qǐng)求及在業(yè)務(wù)中應(yīng)用,AsyncContext是Servlet 3.0使Servlet 線程不再需要一直阻塞,直到業(yè)務(wù)處理完畢才能再輸出響應(yīng),最后才結(jié)束該Servlet線程
    2023-03-03
  • java開發(fā)SpringBoot參數(shù)校驗(yàn)過程示例教程

    java開發(fā)SpringBoot參數(shù)校驗(yàn)過程示例教程

    這篇文章主要為大家介紹了SpringBoot如何進(jìn)行參數(shù)校驗(yàn)的過程示例詳解教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • 最新評(píng)論