基于springmvc之常用注解,操作傳入?yún)?shù)
springmvc常用注解,操作傳入?yún)?shù)
@RequestParam
一般用于jsp參數(shù)名和后臺方法參數(shù)指定,對應(yīng)
/* * value=name 當(dāng)jsp的參數(shù)和方法上的參數(shù)對應(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ù)
測試
@RequestBody
一般用于獲取post請求的方法體,jsp參數(shù)格式為鍵值對,即 key-value
該注解不適應(yīng)于get請求,一般用于post請求,例如表單提交
如果要用于get請求,則需
@RequestBody(required = false)
否則報錯,此時方法參數(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>
測試
@PathVariable
URL的占位符,restful風(fēng)格,傳參格式 url地址后/10
restful請求方式: get,post,put 配合注解@RequestMapping設(shè)置請求方式
@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ā)送不同的請求方式
測試:
@RequestHeader
獲取請求頭的某些屬性值 如瀏覽器類型、版本等 不常用
@RequestMapping(path = "testRequestHeader",method = RequestMethod.GET) /*獲取請求頭的某些屬性值 如瀏覽器類型、版本等*/ 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ù)等場景
可作用于方法和參數(shù)
修飾方法,方法入?yún)⑿韬涂刂破鞣椒ㄍ瑓㈩愋停摲椒▋?yōu)先于控制器之前執(zhí)行,且分類有返回值和無返回值
- 有返回值,則該方法的返回值和控制器的入?yún)⑾嗤嗤?/li>
- 無返回值,則該方法的參數(shù)除了和控制器的入?yún)⑾嗤?,還需加一個map類型參數(shù)map<string,objct>
例子:
注解修飾的方法有返回值寫法
@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ù)不全, 通過名字查詢數(shù)據(jù)庫對應(yīng)的信息 返回全的user對象*/ 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>
注解修飾的方法無返回值寫法
@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ù)不全, 通過名字查詢數(shù)據(jù)庫對應(yīng)的信息 返回全的user對象*/ user.setBirthday(new Date()); userMap.put("key",user); }
測試
@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
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java虛擬機(jī)如何運(yùn)行Java字節(jié)碼
這篇文章主要介紹了Java虛擬機(jī)如何運(yùn)行Java字節(jié)碼的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Java實(shí)現(xiàn)在線五子棋對戰(zhàn)游戲(人機(jī)對戰(zhàn))
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)在線五子棋對戰(zhàn)游戲(人機(jī)對戰(zhàn)),文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以嘗試一下2022-09-09Java利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類
貝葉斯分類算法是統(tǒng)計學(xué)的一種分類方法,它是一類利用概率統(tǒng)計知識進(jìn)行分類的算法。本文將利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類,需要的可以參考一下2022-06-06