SpringMVC獲取請(qǐng)求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼
一、SpringMVC獲取請(qǐng)求參數(shù)
1、通過ServletAPI獲取
將HttpServletRequest作為控制器方法的形參,此時(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、通過控制器方法的形參獲取請(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é)果
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ù),若沒有傳輸該請(qǐng)求參數(shù),且沒有設(shè)置defaultValue屬性,則頁面報(bào)錯(cuò)400:Required String parameter ‘xxx’ is not present;若設(shè)置為false,則當(dāng)前請(qǐng)求不是必須傳輸value所指定的請(qǐng)求參數(shù),若沒有傳輸,則注解所標(biāo)識(shí)的形參的值為null
defaultValue:不管required屬性值為true或false,當(dāng)value所指定的請(qǐng)求參數(shù)沒有傳輸或傳輸?shù)闹禐?quot;"時(shí),則使用默認(rèn)值為形參賦值
4、@RequestHeader
@RequestHeader是將請(qǐng)求頭信息和控制器方法的形參創(chuàng)建映射關(guān)系
@RequestHeader注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam
5、@CookieValue
@CookieValue是將cookie數(shù)據(jù)和控制器方法的形參創(chuàng)建映射關(guān)系
@CookieValue注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam
6、通過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'}
7、解決獲取請(qǐng)求參數(shù)的亂碼問題
解決獲取請(qǐng)求參數(shù)的亂碼問題,可以使用SpringMVC提供的編碼過濾器CharacterEncodingFilter,但是必須在web.xml中進(jìn)行注冊(cè)
<!--配置springMVC的編碼過濾器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注:
SpringMVC中處理編碼的過濾器一定要配置到其他過濾器之前,否則無效
二、域?qū)ο蠊蚕頂?shù)據(jù)
1、使用ServletAPI向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
2、使用ModelAndView向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向請(qǐng)求域共享數(shù)據(jù)
* View主要用于設(shè)置視圖,實(shí)現(xiàn)頁面跳轉(zhuǎn)
*/
ModelAndView mav = new ModelAndView();
//向請(qǐng)求域共享數(shù)據(jù)
mav.addObject("testScope", "hello,ModelAndView");
//設(shè)置視圖,實(shí)現(xiàn)頁面跳轉(zhuǎn)
mav.setViewName("success");
return mav;
}
3、使用Model向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
4、使用map向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
5、使用ModelMap向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
6、Model、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 {}
7、向session域共享數(shù)據(jù)
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}
8、向application域共享數(shù)據(jù)
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}
總結(jié)
以上就是SpringMVC獲取請(qǐng)求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC獲取請(qǐng)求參數(shù)和域?qū)ο蟮馁Y料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文帶你學(xué)會(huì)Java網(wǎng)絡(luò)編程
網(wǎng)絡(luò)編程是指編寫運(yùn)行在多個(gè)設(shè)備(計(jì)算機(jī))的程序,這些設(shè)備都通過網(wǎng)絡(luò)連接起來。這篇文章將帶大家深入了解一下Java的網(wǎng)絡(luò)編程,需要的可以了解一下2022-08-08
java寫卷積神經(jīng)網(wǎng)絡(luò)(CupCnn簡(jiǎn)介)
這篇文章主要介紹了java寫卷積神經(jīng)網(wǎng)絡(luò)(CupCnn簡(jiǎn)介),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
intellij idea快速查看當(dāng)前類中的所有方法(推薦)
這篇文章主要介紹了intellij idea快速查看當(dāng)前類中的所有方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java解決線程的不安全問題之volatile關(guān)鍵字詳解
這篇文章主要介紹了Java解決線程的不安全問題之volatile關(guān)鍵字詳解,可見性指一個(gè)線程對(duì)共享變量值的修改,能夠及時(shí)地被其他線程看到,而 volatile 關(guān)鍵字就保證內(nèi)存的可見性,需要的朋友可以參考下2023-08-08
解決Spring Cloud中Feign/Ribbon第一次請(qǐng)求失敗的方法
這篇文章主要給大家介紹了關(guān)于解決Spring Cloud中Feign/Ribbon第一次請(qǐng)求失敗的方法,文中給出了三種解決的方法,大家可以根據(jù)需要選擇對(duì)應(yīng)的方法,需要的朋友們下面來一起看看吧。2017-02-02
springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
selenium + ChromeDriver安裝及使用方法
這篇文章主要介紹了selenium + ChromeDriver安裝及使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
Java 判斷線程池所有任務(wù)是否執(zhí)行完畢的操作
這篇文章主要介紹了Java 判斷線程池所有任務(wù)是否執(zhí)行完畢的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

