基于springmvc之常用注解,操作傳入?yún)?shù)
springmvc常用注解,操作傳入?yún)?shù)
@RequestParam
一般用于jsp參數(shù)名和后臺(tái)方法參數(shù)指定,對(duì)應(yīng)
/* * value=name 當(dāng)jsp的參數(shù)和方法上的參數(shù)對(duì)應(yīng)不上,可以指明 * required() default true;默認(rèn)true 有參數(shù)則必須傳 * */ public String testRequestParam(@RequestParam(name = "name",required = false) String username){ System.out.println("執(zhí)行了.........."); System.out.println(username); return "success"; }
<body> <a href="anno/testRequestParam" rel="external nofollow" rel="external nofollow" >testRequestParam</a> </body>
不傳參數(shù),required()設(shè)置為false,方法有參數(shù)
測(cè)試
@RequestBody
一般用于獲取post請(qǐng)求的方法體,jsp參數(shù)格式為鍵值對(duì),即 key-value
該注解不適應(yīng)于get請(qǐng)求,一般用于post請(qǐng)求,例如表單提交
如果要用于get請(qǐng)求,則需
@RequestBody(required = false)
否則報(bào)錯(cuò),此時(shí)方法參數(shù)為null
@RequestMapping(path = "testRequestBody") public String testRequestBody(@RequestBody(required = false) String body){ System.out.println("執(zhí)行了.........."); System.out.println(body); return "success"; }
<body> <%--<a href="anno/testRequestParam" rel="external nofollow" rel="external nofollow" >testRequestParam</a>--%> <form action="anno/testRequestBody" method="post"> 用戶名:<input type="text" name="username"/><br> 密碼:<input type="text" name="password"/><br> <input type="submit" value="提交"/><br> </form> </body>
測(cè)試
@PathVariable
URL的占位符,restful風(fēng)格,傳參格式 url地址后/10
restful請(qǐng)求方式: get,post,put 配合注解@RequestMapping設(shè)置請(qǐng)求方式
@RequestMapping(path = "testPathVariable/{sid}",method = RequestMethod.GET)
@RequestMapping(path = "testPathVariable/{sid}",method = RequestMethod.GET) /* * {sid}表示URL的占位符 * boolean required() default true;默認(rèn)參數(shù)必須傳 * */ public String testPathVariable(@PathVariable("sid") String id){ System.out.println("執(zhí)行了.........."); System.out.println(id); return "success"; }
<a href="anno/testPathVariable/10" rel="external nofollow" >testPathVariable</a>
可以下載postman客戶端,模擬發(fā)送不同的請(qǐng)求方式
測(cè)試:
@RequestHeader
獲取請(qǐng)求頭的某些屬性值 如瀏覽器類型、版本等 不常用
@RequestMapping(path = "testRequestHeader",method = RequestMethod.GET) /*獲取請(qǐng)求頭的某些屬性值 如瀏覽器類型、版本等*/ public String testRequestHeader(@RequestHeader(value = "Accept") String head){ System.out.println("執(zhí)行了.........."); System.out.println(head); return "success"; }
<a href="anno/testRequestHeader" rel="external nofollow" >testRequestHeader</a>
@CookieValue
獲取JSESSIONID的值
@RequestMapping(path = "testCookieValue",method = RequestMethod.GET) public String testCookieValue(@CookieValue(value = "JSESSIONID") String JSESSIONID){ System.out.println("執(zhí)行了.........."); System.out.println(JSESSIONID); return "success"; }
<a href="anno/testCookieValue" rel="external nofollow" >testCookieValue</a><br>
@ModelAttribute
用于封裝的數(shù)據(jù)不全補(bǔ)全數(shù)據(jù),或者檢查封裝數(shù)據(jù)等場(chǎng)景
可作用于方法和參數(shù)
修飾方法,方法入?yún)⑿韬涂刂破鞣椒ㄍ瑓㈩愋?,該方法?yōu)先于控制器之前執(zhí)行,且分類有返回值和無(wú)返回值
- 有返回值,則該方法的返回值和控制器的入?yún)⑾嗤嗤?/li>
- 無(wú)返回值,則該方法的參數(shù)除了和控制器的入?yún)⑾嗤猓€需加一個(gè)map類型參數(shù)map<string,objct>
例子:
注解修飾的方法有返回值寫(xiě)法
@RequestMapping(path = "testModelAttribute") public String testModelAttribute(User user){ System.out.println("執(zhí)行了.........."); System.out.println(user); return "success"; } @ModelAttribute //修飾方法,該方法優(yōu)先于控制器之前執(zhí)行 public User showUser(User user){ /*模擬jsp傳的user封裝數(shù)據(jù)不全, 通過(guò)名字查詢數(shù)據(jù)庫(kù)對(duì)應(yīng)的信息 返回全的user對(duì)象*/ user.setBirthday(new Date()); return user; }
<form action="anno/testModelAttribute" method="post"> 用戶名:<input type="text" name="uname"/><br> 年齡:<input type="text" name="age"/><br> <input type="submit" value="提交"/><br> </form>
注解修飾的方法無(wú)返回值寫(xiě)法
@RequestMapping(path = "testModelAttribute") public String testModelAttribute(@ModelAttribute("key") User user){ System.out.println("執(zhí)行了.........."); System.out.println(user); return "success"; } @ModelAttribute //修飾方法,該方法優(yōu)先于控制器之前執(zhí)行 public void showUser(User user, Map<String,User> userMap){ /*模擬jsp傳的user封裝數(shù)據(jù)不全, 通過(guò)名字查詢數(shù)據(jù)庫(kù)對(duì)應(yīng)的信息 返回全的user對(duì)象*/ user.setBirthday(new Date()); userMap.put("key",user); }
測(cè)試
@SessionAttributes
注解只能作用于類,用于存取數(shù)據(jù)到session域?qū)ο笾?,?shí)現(xiàn)方法數(shù)據(jù)共享
實(shí)現(xiàn)方式:從request域?qū)ο笾袕?fù)制數(shù)據(jù)到session域中
/** * @Date 2019/9/12 2:05 * by mocar */ @Controller @RequestMapping(path = "/anno") @SessionAttributes(names = {"msg"})//從request域?qū)ο笾袕?fù)制到session域?qū)ο? public class annoController { @RequestMapping("/setRequest")//存入 public String setRequest(ModelMap modelMap){ System.out.println("setRequest......"); modelMap.addAttribute("msg","test");//往Request域?qū)ο蟠嬷? return "success"; } @RequestMapping("/getSession")//獲取 public String getSession(ModelMap modelMap){ System.out.println("getSession......."); Object msg = modelMap.get("msg"); System.out.println(msg.toString()); return "success"; } @RequestMapping("/delSession")//刪除 public String delSession(SessionStatus sessionStatus,ModelMap modelMap){ System.out.println("delSession......."); sessionStatus.setComplete(); Object msg = modelMap.get("msg"); System.out.println(msg.toString()); return "success"; } }
jsp:
<br> <a href="anno/setRequest" rel="external nofollow" >setRequest</a><br> <a href="anno/getSession" rel="external nofollow" >getSession</a><br> <a href="anno/delSession" rel="external nofollow" >delSession</a><br>
success.jsp 設(shè)置不忽略EL表達(dá)式,顯示session域數(shù)據(jù)
<%-- Created by IntelliJ IDEA. User: Mocar Date: 2019/9/11 Time: 4:34 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>快速入門</title> </head> <body> <h3>success</h3> ${sessionScope} </body> </html>
setsession
getsession
delsession
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyEclipse開(kāi)發(fā)一個(gè)webservice接口
這篇文章主要為大家詳細(xì)介紹了MyEclipse開(kāi)發(fā)一個(gè)webservice接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07java實(shí)現(xiàn)網(wǎng)頁(yè)解析示例
這篇文章主要介紹了java實(shí)現(xiàn)網(wǎng)頁(yè)解析示例,需要的朋友可以參考下2014-04-04微信開(kāi)發(fā)--自定義菜單查詢返碼亂碼的解決方法
本篇文章主要介紹了微信開(kāi)發(fā)--自定義菜單查詢返碼亂碼的解決方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03Java虛擬機(jī)如何運(yùn)行Java字節(jié)碼
這篇文章主要介紹了Java虛擬機(jī)如何運(yùn)行Java字節(jié)碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Java實(shí)現(xiàn)在線五子棋對(duì)戰(zhàn)游戲(人機(jī)對(duì)戰(zhàn))
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)在線五子棋對(duì)戰(zhàn)游戲(人機(jī)對(duì)戰(zhàn)),文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以嘗試一下2022-09-09Java利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類
貝葉斯分類算法是統(tǒng)計(jì)學(xué)的一種分類方法,它是一類利用概率統(tǒng)計(jì)知識(shí)進(jìn)行分類的算法。本文將利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類,需要的可以參考一下2022-06-06