基于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>快速入門(mén)</title>
</head>
<body>
<h3>success</h3>
${sessionScope}
</body>
</html>
setsession

getsession

delsession

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Struts2學(xué)習(xí)筆記(2)-路徑問(wèn)題解決
本文主要介紹Struts2的路徑問(wèn)題,盡量不要使用相對(duì)路徑,使用相對(duì)路徑會(huì)讓路徑問(wèn)題變得很繁瑣很麻煩,推薦使用絕對(duì)路徑,希望能給大家做一個(gè)參考。2016-06-06
Java多線程常見(jiàn)案例分析線程池與單例模式及阻塞隊(duì)列
這篇文章主要介紹了多線程的常見(jiàn)案例,線程池(重點(diǎn))、單例模式、阻塞隊(duì)列,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Spring MVC+FastJson+hibernate-validator整合的完整實(shí)例教程
這篇文章主要給大家介紹了關(guān)于Spring MVC+FastJson+hibernate-validator整合的完整實(shí)例教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
Java數(shù)據(jù)結(jié)構(gòu)之ArrayList從順序表到實(shí)現(xiàn)
Java中的ArrayList是一種基于數(shù)組實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu),支持動(dòng)態(tài)擴(kuò)容和隨機(jī)訪問(wèn)元素,可用于實(shí)現(xiàn)順序表等數(shù)據(jù)結(jié)構(gòu)。ArrayList在內(nèi)存中連續(xù)存儲(chǔ)元素,支持快速的隨機(jī)訪問(wèn)和遍歷。通過(guò)學(xué)習(xí)ArrayList的實(shí)現(xiàn)原理和使用方法,可以更好地掌握J(rèn)ava中的數(shù)據(jù)結(jié)構(gòu)和算法2023-04-04
將Java項(xiàng)目打包成可執(zhí)行的jar包
這篇文章主要介紹了將Java項(xiàng)目打包成可執(zhí)行的jar包,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
spring boot加入攔截器Interceptor過(guò)程解析
這篇文章主要介紹了spring boot加入攔截器Interceptor過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Spring profile通過(guò)多種方法實(shí)現(xiàn)多環(huán)境支持
這篇文章主要介紹了Spring profile通過(guò)多種方法實(shí)現(xiàn)多環(huán)境支持,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Java class文件格式之特殊字符串_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
特殊字符串出現(xiàn)在class文件中的常量池中,本著循序漸進(jìn)和減少跨度的原則, 首先把class文件中的特殊字符串做一個(gè)詳細(xì)的介紹, 然后再回過(guò)頭來(lái)繼續(xù)講解常量池,對(duì)java class 文件格式相關(guān)知識(shí)感興趣的的朋友一起學(xué)習(xí)吧2017-06-06

