SpringMVC獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼
一、SpringMVC獲取請求參數(shù)
1、通過ServletAPI獲取
將HttpServletRequest作為控制器方法的形參,此時(shí)HttpServletRequest類型的參數(shù)表示封裝了當(dāng)前請求的請求報(bào)文的對象
@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、通過控制器方法的形參獲取請求參數(shù)
在控制器方法的形參位置,設(shè)置和請求參數(shù)同名的形參,當(dāng)瀏覽器發(fā)送請求,匹配到請求映射時(shí),在DispatcherServlet中就會(huì)將請求參數(shù)賦值給相應(yīng)的形參
<a th:href="@{/testParam(username='admin',password=123456)}" rel="external nofollow" >測試獲取請求參數(shù)-->/testParam</a><br>
@RequestMapping("/testParam") public String testParam(String username, String password){ System.out.println("username:"+username+",password:"+password); return "success"; }
注:
若請求所傳輸?shù)恼埱髤?shù)中有多個(gè)同名的請求參數(shù),此時(shí)可以在控制器方法的形參中設(shè)置字符串?dāng)?shù)組或者字符串類型的形參接收此請求參數(shù)
若使用字符串?dāng)?shù)組類型的形參,此參數(shù)的數(shù)組中包含了每一個(gè)數(shù)據(jù)
若使用字符串類型的形參,此參數(shù)的值為每個(gè)數(shù)據(jù)中間使用逗號拼接的結(jié)果
3、@RequestParam
@RequestParam是將請求參數(shù)和控制器方法的形參創(chuàng)建映射關(guān)系
@RequestParam注解一共有三個(gè)屬性:
value:指定為形參賦值的請求參數(shù)的參數(shù)名
required:設(shè)置是否必須傳輸此請求參數(shù),默認(rèn)值為true
若設(shè)置為true時(shí),則當(dāng)前請求必須傳輸value所指定的請求參數(shù),若沒有傳輸該請求參數(shù),且沒有設(shè)置defaultValue屬性,則頁面報(bào)錯(cuò)400:Required String parameter ‘xxx’ is not present;若設(shè)置為false,則當(dāng)前請求不是必須傳輸value所指定的請求參數(shù),若沒有傳輸,則注解所標(biāo)識的形參的值為null
defaultValue:不管required屬性值為true或false,當(dāng)value所指定的請求參數(shù)沒有傳輸或傳輸?shù)闹禐?quot;"時(shí),則使用默認(rèn)值為形參賦值
4、@RequestHeader
@RequestHeader是將請求頭信息和控制器方法的形參創(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獲取請求參數(shù)
可以在控制器方法的形參位置設(shè)置一個(gè)實(shí)體類類型的形參,此時(shí)若瀏覽器傳輸?shù)恼埱髤?shù)的參數(shù)名和實(shí)體類中的屬性名一致,那么請求參數(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、解決獲取請求參數(shù)的亂碼問題
解決獲取請求參數(shù)的亂碼問題,可以使用SpringMVC提供的編碼過濾器CharacterEncodingFilter,但是必須在web.xml中進(jìn)行注冊
<!--配置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主要用于向請求域共享數(shù)據(jù) * View主要用于設(shè)置視圖,實(shí)現(xiàn)頁面跳轉(zhuǎn) */ ModelAndView mav = new ModelAndView(); //向請求域共享數(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獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC獲取請求參數(shù)和域?qū)ο蟮馁Y料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決
這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06Java實(shí)現(xiàn)定時(shí)器的4種方法超全總結(jié)
對于一些特殊的代碼是需要定時(shí)執(zhí)行的,下面來看看定時(shí)器該如何編寫吧,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)定時(shí)器的4種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05JAVA SpringBoot jar程序 Systemctl生產(chǎn)環(huán)境部署方案
這篇文章主要介紹了JAVA SpringBoot jar程序 Systemctl生產(chǎn)環(huán)境部署方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03Java后端服務(wù)間歇性響應(yīng)慢的問題排查與解決
之前在公司內(nèi)其它團(tuán)隊(duì)找到幫忙排查的一個(gè)后端服務(wù)連接超時(shí)問題,問題的表現(xiàn)是服務(wù)部署到線上后出現(xiàn)間歇性請求響應(yīng)非常慢(大于10s),但是后端業(yè)務(wù)分析業(yè)務(wù)日志時(shí)卻沒有發(fā)現(xiàn)慢請求,所以本文給大家介紹了Java后端服務(wù)間歇性響應(yīng)慢的問題排查與解決,需要的朋友可以參考下2025-03-03