SpringMVC獲取請求參數(shù)筆記整理
前言
本篇文章目的是為了學(xué)習(xí)、記錄和分享
博主在學(xué)習(xí) Spring MVC
過程中的筆記。同時也希望本篇文章能夠幫助屏幕前的你!
一、使用ServletAPI獲取參數(shù)
通過 HttpServletRequest 當(dāng)作形參,此時 HttpServletRequest 類型的參數(shù)表示封裝了當(dāng)前請求的請求報文的對象。
測試案例:
@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"; }
<form method="get" action="/SpringMVC/testParam"> username: <input type="text" name="username"/> password: <input type="password" name="password"/> <input type="submit" value="Submit" /> </form>
運(yùn)行結(jié)果:
二、通過控制器方法的形參獲取請求參數(shù)
在控制器方法的形參位置,設(shè)置和請求參數(shù)同名的形參,當(dāng)瀏覽器發(fā)送請求,匹配到請求映射時,在 DispatcherServlet 中就會將請求參數(shù)賦值給相應(yīng)的形參。
測試用例:
@RequestMapping("/testParam") public String testParam(String username,String password){ System.out.println("username:"+username+",password:"+password); return "success"; }
<form method="get" action="/SpringMVC/testParam"> username: <input type="text" name="username"/> password: <input type="password" name="password"/> <input type="submit" value="Submit" /> </form>
運(yùn)行結(jié)果:
三、@RequestParam
@RequestParam是將請求參數(shù)
和控制器方法的形參
創(chuàng)建映射關(guān)系!
OK,我們可以來看一下 @RequestParam 的源碼部分:
@Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestParam { //別名name,一般默認(rèn)為空 @AliasFor("name") String value() default ""; //指定為形參賦值的請求參數(shù)的參數(shù)名 @AliasFor("value") String name() default ""; //設(shè)置是否必須傳輸此請求參數(shù),默認(rèn)值為true boolean required() default true; //用來定義默認(rèn)值 String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n"; }
注:
若required
設(shè)置為true
時,則當(dāng)前請求必須傳輸value
所指定的請求參數(shù),若沒有傳輸該請求參數(shù),且沒有設(shè)置defaultValue
屬性,則頁面報錯 400:Required String parameter ‘xxx’ is not present; 若設(shè)置為false
,則當(dāng)前請求不是必須傳輸value
所指定的請求參數(shù),若沒有傳輸,則注解所標(biāo)識的形參的值為null
。
@RequestMapping(value = "/testParam", method = RequestMethod.POST) public String testParam(@RequestParam(value = "user_name",required = false,defaultValue = "gg") String name, @RequestParam(value = "password",required = true,defaultValue = "hh") String pwd, String[] hobby) { System.out.println(name + pwd + Arrays.toString(hobby)); return "success"; }
<form th:action="@{/testParam}" method="post"> 用戶名:<input type="name" name = "user_name"><br> 密碼:<input type="password" name = "password"><br> 愛好:<input type="checkbox" name = "hobby" value="a">a <input type="checkbox" name = "hobby" value="b">b <input type="checkbox" name = "hobby" value="c">c<br> <input type="submit" value="測試"> </form>
運(yùn)行結(jié)果:
四、@RequestHeader
@RequestHeader
是將請求頭信息和控制器方法的形參創(chuàng)建映射關(guān)系。
@RequestHeader
注解一共有三個屬性:value、required、defaultValue,用法同@RequestParam
。
@RequestMapping(value = "/testParam2",method = RequestMethod.POST) //形參位置的request表示當(dāng)前請求 public String testParam2(@RequestParam("username") String name, @RequestParam("password") String pwd, String[] hobby, @RequestHeader("Host") String host){ System.out.println(name+pwd+ Arrays.toString(hobby)); System.out.println("Host:"+host);//輸出結(jié)果Host:localhost:8080 return "success"; }
五、@CookieValue
@CookieValue
是將cookie
數(shù)據(jù)和控制器方法的形參創(chuàng)建映射關(guān)系
@CookieValue
注解一共有三個屬性:value、required、defaultValue,用法同@RequestParam
@CookieValue("JSESSIONID") String JSESSIONID
@Controller @RequestMapping("hello") public class HelloController2 { ? ? @RequestMapping("show25") ? ? public String test25(Model model, @CookieValue("JSESSIONID")String jsessionid){ ? ? ? ? model.addAttribute("msg", "獲取cookie,jsessionid:" + jsessionid); ? ? ? ? return "hello2"; ? ? } }
六、通過實(shí)體類的形參獲取參數(shù)
首先需要創(chuàng)建一個實(shí)體類 User
package xiaobao.mvc.bean; public class User { private Integer id; private String username; private String password; private Integer age; private String sex; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; public String getUsername() { return username; public void setUsername(String username) { this.username = username; public String getPassword() { return password; public void setPassword(String password) { this.password = password; public Integer getAge() { return age; public void setAge(Integer age) { this.age = age; public String getSex() { return sex; public void setSex(String sex) { this.sex = sex; public String getEmail() { return email; public void setEmail(String email) { this.email = email; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", email='" + email + '\'' + '}'; }
然后前端:
<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"; }
運(yùn)行結(jié)果:
因?yàn)槲疫€沒沒有解決亂碼,所以這個就一直亂碼問題,等到后面我專門出一期這個文章來解決亂碼的問題!
到此這篇關(guān)于SpringMVC獲取請求參數(shù)筆記整理的文章就介紹到這了,更多相關(guān)SpringMVC獲取請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot如何獲取配置文件application.yml中自定義的變量并使用
這篇文章主要介紹了Springboot中獲取配置文件(application.yml)中自定義的變量并使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Swing中依據(jù)鼠標(biāo)拖拽來畫出矩形的實(shí)現(xiàn)方法
這篇文章主要介紹了Swing中依據(jù)鼠標(biāo)拖拽來畫出矩形的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11解析分別用遞歸與循環(huán)的方式求斐波那契數(shù)列的實(shí)現(xiàn)方法
本篇文章是對分別用遞歸與循環(huán)的方式求斐波那契數(shù)列的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06基于RxPaparazzo實(shí)現(xiàn)圖片裁剪、圖片旋轉(zhuǎn)、比例放大縮小功能
這篇文章主要為大家詳細(xì)介紹了基于RxPaparazzo實(shí)現(xiàn)圖片裁剪、圖片旋轉(zhuǎn)、比例放大縮小功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05spring @Validated 注解開發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05