詳解SpringMVC常用注解功能及屬性
1.@RequestMapping注解
1.1@RequestMapping注解的功能
從注解名稱上我們可以看到,@RequestMapping注解的作用就是將請(qǐng)求和處理請(qǐng)求的控制器方法關(guān)聯(lián)起來(lái),建立映射關(guān)系。
SpringMVC 接收到指定的請(qǐng)求,就會(huì)來(lái)找到在映射關(guān)系中對(duì)應(yīng)的控制器方法來(lái)處理這個(gè)請(qǐng)求。
1.2@RequestMapping注解的位置
@RequestMapping標(biāo)識(shí)一個(gè)類:設(shè)置映射請(qǐng)求的請(qǐng)求路徑的初始信息
@RequestMapping標(biāo)識(shí)一個(gè)方法:設(shè)置映射請(qǐng)求請(qǐng)求路徑的具體信息
@Controller
@RequestMapping("/test")
public class RequestMappingController {
//此時(shí)請(qǐng)求映射所映射的請(qǐng)求的請(qǐng)求路徑為:/test/testRequestMapping
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
return "success";
}
}
1.3@RequestMapping注解的value屬性
@RequestMapping注解的value屬性通過(guò)請(qǐng)求的請(qǐng)求地址匹配請(qǐng)求映射
@RequestMapping注解的value屬性是一個(gè)字符串類型的數(shù)組,表示該請(qǐng)求映射能夠匹配多個(gè)請(qǐng)求地址所對(duì)應(yīng)的請(qǐng)求
@RequestMapping注解的value屬性必須設(shè)置,至少通過(guò)請(qǐng)求地址匹配請(qǐng)求映射
<a th:href="@{/testRequestMapping}" rel="external nofollow" >測(cè)試@RequestMapping的value屬性-->/testRequestMapping</a><br>
<a th:href="@{/test}" rel="external nofollow" rel="external nofollow" >測(cè)試@RequestMapping的value屬性-->/test</a><br>
@RequestMapping(
value = {"/testRequestMapping", "/test"}
)
public String testRequestMapping(){
return "success";
}
1.4@RequestMapping注解的method屬性
@RequestMapping注解的method屬性通過(guò)請(qǐng)求的請(qǐng)求方式(get或post)匹配請(qǐng)求映射
@RequestMapping注解的method屬性是一個(gè)RequestMethod類型的數(shù)組,表示該請(qǐng)求映射能夠匹配多種請(qǐng)求方式的請(qǐng)求
若當(dāng)前請(qǐng)求的請(qǐng)求地址滿足請(qǐng)求映射的value屬性,但是請(qǐng)求方式不滿足method屬性,則瀏覽器報(bào)錯(cuò)
405:Request method 'POST' not supported
<a th:href="@{/test}" rel="external nofollow" rel="external nofollow" >測(cè)試@RequestMapping的value屬性-->/test</a><br>
<form th:action="@{/test}" method="post">
<input type="submit">
</form>
@RequestMapping(
value = {"/testRequestMapping", "/test"},
method = {RequestMethod.GET, RequestMethod.POST}
)
public String testRequestMapping(){
return "success";
}
注意:
(1)對(duì)于處理指定請(qǐng)求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解:
處理get請(qǐng)求的映射–>@GetMapping
處理post請(qǐng)求的映射–>@PostMapping
處理put請(qǐng)求的映射–>@PutMapping
處理delete請(qǐng)求的映射–>@DeleteMapping
(2)常用的請(qǐng)求方式有get,post,put,delete
但是目前瀏覽器只支持get和post,若在form表單提交時(shí),為method設(shè)置了其他請(qǐng)求方式的字符串(put或delete),則按照默認(rèn)的請(qǐng)求方式get處理。
若要發(fā)送put和delete請(qǐng)求,則需要通過(guò)spring提供的過(guò)濾器HiddenHttpMethodFilter。
1.5@RequestMapping注解的params屬性(了解)
@RequestMapping注解的params屬性通過(guò)請(qǐng)求的請(qǐng)求參數(shù)匹配請(qǐng)求映射
@RequestMapping注解的params屬性是一個(gè)字符串類型的數(shù)組,可以通過(guò)四種表達(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
<a th:href="@{/test(username='admin',password=123456)" rel="external nofollow" >測(cè)試@RequestMapping的params屬性-->/test</a><br>
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username","password!=123456"}
)
public String testRequestMapping(){
return "success";
}
注意:
若當(dāng)前請(qǐng)求滿足@RequestMapping注解的value和method屬性,但是不滿足params屬性,此時(shí)頁(yè)面會(huì)報(bào)錯(cuò)
400:Parameter conditions "username, password!=123456" not met for actual request parameters: username={admin}, password={123456}
1.6@RequestMapping注解的headers屬性(了解)
@RequestMapping注解的headers屬性通過(guò)請(qǐng)求的請(qǐng)求頭信息匹配請(qǐng)求映射
@RequestMapping注解的headers屬性是一個(gè)字符串類型的數(shù)組,可以通過(guò)四種表達(dá)式設(shè)置請(qǐng)求頭信息和請(qǐng)求映射的匹配關(guān)系
"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í)頁(yè)面顯示404錯(cuò)誤,即資源未找到。
1.7SpringMVC支持路徑中的占位符(@PathVariable)(重點(diǎn))
原始方式:/deleteUser?id=1
rest方式:/deleteUser/1
SpringMVC路徑中的占位符常用于RESTful風(fēng)格中,當(dāng)請(qǐng)求路徑中將某些數(shù)據(jù)通過(guò)路徑的方式傳輸?shù)椒?wù)器中,就可以在相應(yīng)的@RequestMapping注解的value屬性中通過(guò)占位符{xxx}表示傳輸?shù)臄?shù)據(jù),在通過(guò)==@PathVariable==注解,將占位符所表示的數(shù)據(jù)賦值給控制器方法的形參
<a th:href="@{/testRest/1/admin}" rel="external nofollow" >測(cè)試路徑中的占位符-->/testRest</a><br>
@RequestMapping("/testRest/{id}/{username}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}
//最終輸出的內(nèi)容為-->id:1,username:admin
2.SpringMVC獲取請(qǐng)求參數(shù)
2.1通過(guò)ServletAPI獲?。私猓?/h3>
將HttpServletReques作為控制器方法的形參,此時(shí)HttpServletRequest類型的參數(shù)表示封裝了當(dāng)前請(qǐng)求的請(qǐng)求報(bào)文的對(duì)象
@RequestMapping("/testParam")
public String testParam(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username:"+username+",password:"+password);
return "success";
}
2.2通過(guò)控制器方法的形參獲取請(qǐng)求參數(shù)
在控制器方法的形參位置,設(shè)置和請(qǐng)求參數(shù)同名的形參,當(dāng)瀏覽器發(fā)送請(qǐng)求,匹配到請(qǐng)求映射時(shí),在DispatcherServlet中就會(huì)將請(qǐng)求參數(shù)賦值給相應(yīng)的形參
<a th:href="@{/testParam(username='admin',password=123456)}" rel="external nofollow" >測(cè)試獲取請(qǐng)求參數(shù)-->/testParam</a><br>
@RequestMapping("/testParam")
public String testParam(String username, String password){
System.out.println("username:"+username+",password:"+password);
return "success";
}
注:
若請(qǐng)求所傳輸?shù)恼?qǐng)求參數(shù)中有多個(gè)同名的請(qǐng)求參數(shù),此時(shí)可以在控制器方法的形參中設(shè)置字符串?dāng)?shù)組或者字符串類型的形參接收此請(qǐng)求參數(shù)
若使用字符串?dāng)?shù)組類型的形參,此參數(shù)的數(shù)組中包含了每一個(gè)數(shù)據(jù)
若使用字符串類型的形參,此參數(shù)的值為每個(gè)數(shù)據(jù)中間使用逗號(hào)拼接的結(jié)果
2.3@RequestParam
@RequestParam是將請(qǐng)求參數(shù)和控制器方法的形參創(chuàng)建映射關(guān)系
@RequestParam注解一共有三個(gè)屬性:
value:指定為形參賦值的請(qǐng)求參數(shù)的參數(shù)名
required:設(shè)置是否必須傳輸此請(qǐng)求參數(shù),默認(rèn)值為true
若設(shè)置為true時(shí),則當(dāng)前請(qǐng)求必須傳輸value所指定的請(qǐng)求參數(shù),若沒(méi)有傳輸該請(qǐng)求參數(shù),且沒(méi)有設(shè)置defaultValue屬性,則頁(yè)面報(bào)錯(cuò)
400:Required String parameter 'xxx' is not present;
若設(shè)置為false,則當(dāng)前請(qǐng)求不是必須傳輸value所指定的請(qǐng)求參數(shù),若沒(méi)有傳輸,則注解所標(biāo)識(shí)的形參的值為null
defaultValue:不管required屬性值為true或false,當(dāng)value所指定的請(qǐng)求參數(shù)沒(méi)有傳輸或傳輸?shù)闹禐?"時(shí),則使用默認(rèn)值為形參賦值
2.4@RequestHeader
@RequestHeader是將請(qǐng)求頭信息和控制器方法的形參創(chuàng)建映射關(guān)系
@RequestHeader注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam
2.5@CookieValue
@CookieValue是將**cookie數(shù)據(jù)**和控制器方法的形參創(chuàng)建映射關(guān)系
@CookieValue注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam
2.6通過(guò)POJO獲取請(qǐng)求參數(shù)
可以在控制器方法的形參位置設(shè)置一個(gè)實(shí)體類類型的形參,此時(shí)若瀏覽器傳輸?shù)恼?qǐng)求參數(shù)的參數(shù)名和實(shí)體類中的屬性名一致,那么請(qǐng)求參數(shù)就會(huì)為此屬性賦值
<form th:action="@{/testpojo}" method="post">
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
性別:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
年齡:<input type="text" name="age"><br>
/.// 郵箱:<input type="text" name="email"><br>
<input type="submit">
</form>
@RequestMapping("/testpojo")
public String testPOJO(User user){
System.out.println(user);
return "success";
}
//最終結(jié)果-->User{id=null, username='張三', password='123', age=23, sex='男', email='123@qq.com'}
3.域?qū)ο蠊蚕頂?shù)據(jù)
3.1使用ServletAPI向request域?qū)ο蠊蚕頂?shù)據(jù)(了解)
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
3.2使用ModelAndView向request域?qū)ο蠊蚕頂?shù)據(jù)
ModelAndView有Model和View的功能:
Model主要用于向請(qǐng)求域共享數(shù)據(jù)
View主要用于設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向請(qǐng)求域共享數(shù)據(jù)
* View主要用于設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
*/
ModelAndView mav = new ModelAndView();
//向請(qǐng)求域共享數(shù)據(jù)
mav.addObject("testScope", "hello,ModelAndView");
//設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
mav.setViewName("success");
return mav;
}
3.3使用Model向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
3.4使用map向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
3.5使用ModelMap向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
3.6Model、ModelMap、Map的關(guān)系
Model、ModelMap、Map類型的參數(shù)其實(shí)本質(zhì)上都是 BindingAwareModelMap 類型的
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
3.7向session域共享數(shù)據(jù)
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}
3.8向application域共享數(shù)據(jù)
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}
4.HttpMessageConverter
HttpMessageConverter:報(bào)文信息轉(zhuǎn)換器,將請(qǐng)求報(bào)文轉(zhuǎn)換為Java對(duì)象,或?qū)ava對(duì)象轉(zhuǎn)換為響應(yīng)報(bào)文
HttpMessageConverter提供了兩個(gè)注解和兩個(gè)類型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity
4.1@RequestBody
@RequestBody可以獲取請(qǐng)求體,需要在控制器方法設(shè)置一個(gè)形參,使用@RequestBody進(jìn)行標(biāo)識(shí),當(dāng)前請(qǐng)求的請(qǐng)求體就會(huì)為當(dāng)前注解所標(biāo)識(shí)的形參賦值
<form th:action="@{/testRequestBody}" method="post">
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
<input type="submit">
</form>
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String requestBody){
System.out.println("requestBody:"+requestBody);
return "success";
}
/*輸出結(jié)果:
requestBody:username=admin&password=123456
*/
4.2RequestEntity
RequestEntity封裝請(qǐng)求報(bào)文的一種類型,需要在控制器方法的形參中設(shè)置該類型的形參,當(dāng)前請(qǐng)求的請(qǐng)求報(bào)文就會(huì)賦值給該形參,可以通過(guò)getHeaders()獲取請(qǐng)求頭信息,通過(guò)getBody()獲取請(qǐng)求體信息
@RequestMapping("/testRequestEntity")
public String testRequestEntity(RequestEntity<String> requestEntity){
System.out.println("requestHeader:"+requestEntity.getHeaders());
System.out.println("requestBody:"+requestEntity.getBody());
return "success";
}
輸出結(jié)果:
requestHeader:[host:"localhost:8080", connection:"keep-alive", content-length:"27", cache-control:"max-age=0", sec-ch-ua:"" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"", sec-ch-ua-mobile:"?0", upgrade-insecure-requests:"1", origin:"http://localhost:8080", user-agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"] requestBody:username=admin&password=123
4.3@ResponseBody
@ResponseBody用于標(biāo)識(shí)一個(gè)控制器方法,可以將該方法的返回值直接作為響應(yīng)報(bào)文的響應(yīng)體響應(yīng)到瀏覽器
@RequestMapping("/testResponseBody")
@ResponseBody
public String testResponseBody(){
return "success";
}
結(jié)果:瀏覽器頁(yè)面顯示success
4.4@RestController
@RestController注解是springMVC提供的一個(gè)復(fù)合注解,標(biāo)識(shí)在控制器的類上,就相當(dāng)于為類添加了@Controller注解,并且為其中的每個(gè)方法添加了@ResponseBody注解
4.5ResponseEntity
ResponseEntity用于控制器方法的返回值類型,該控制器方法的返回值就是響應(yīng)到瀏覽器的響應(yīng)報(bào)文
以上就是詳解SpringMVC注解功能及屬性的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC注解功能及屬性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java類之間的關(guān)系圖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
在Java以及其他的面向?qū)ο笤O(shè)計(jì)模式中,類與類之間主要有6種關(guān)系,他們分別是:依賴、關(guān)聯(lián)、聚合、組合、繼承、實(shí)現(xiàn)。他們的耦合度依次增強(qiáng),有興趣的可以了解一下2017-08-08
使用Java 8中的Lambda表達(dá)式實(shí)現(xiàn)工廠模式
這篇文章主要給大家介紹了使用Java 8中的Lambda表達(dá)式實(shí)現(xiàn)工廠模式的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04
Spring boot打包jar分離lib和resources方法實(shí)例
這篇文章主要介紹了Spring boot打包jar分離lib和resources方法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
spring boot實(shí)現(xiàn)上傳圖片并在頁(yè)面上顯示及遇到的問(wèn)題小結(jié)
最近在使用spring boot搭建網(wǎng)站的過(guò)程之中遇到了有點(diǎn)小問(wèn)題,最終解決方案是在main目錄下新建了一個(gè)webapp文件夾,并且對(duì)其路徑進(jìn)行了配置,本文重點(diǎn)給大家介紹spring boot實(shí)現(xiàn)上傳圖片并在頁(yè)面上顯示功能,需要的朋友參考下吧2017-12-12
基于SpringBoot實(shí)現(xiàn)QQ郵箱驗(yàn)證碼注冊(cè)功能
QQ 郵箱是由騰訊公司推出的一款免費(fèi)郵箱服務(wù),它提供了完整的郵件發(fā)送和接收功能,并且還支持多種郵件格式和附件類型,QQ 郵箱還具有強(qiáng)大的反垃圾郵件功能,可以有效地過(guò)濾垃圾郵件,并保護(hù)用戶隱私和安全,所以本文給大家介紹了基于SpringBoot實(shí)現(xiàn)QQ郵箱驗(yàn)證碼注冊(cè)功能2024-11-11
Java設(shè)計(jì)模式之監(jiān)聽(tīng)器模式實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之監(jiān)聽(tīng)器模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了java設(shè)計(jì)模式中監(jiān)聽(tīng)器模式的概念、原理及相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-02-02

