前言
下面用到了thymeleaf,不知道的可以看我同專欄里的搭建框架這篇文章。
一、通過ServletAPI獲取
將HttpServletRequest作為控制器方法的形參,此時HttpServletRequest類型的參數(shù)表示封裝了當(dāng)前請求的請求報文的對象。
<a th:href="@{/testServletAPI(username='admin',password=123456)}">測試使用servletAPI獲取請求參數(shù)</a><br>
@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";
}
二、通過控制器方法的形參獲取請求參數(shù)
在控制器方法的形參位置,設(shè)置和請求參數(shù)同名的形參,當(dāng)瀏覽器發(fā)送請求,匹配到請求映射時,在DispatcherServlet中就會將請求參數(shù)賦值給相應(yīng)的形參。
在springMVC中直接在控制層里的映射方法中添加參數(shù)直接獲取請求參數(shù)。
<a th:href="@{/testParam(username='admin',password=123456)}">測試獲取請求參數(shù)--
>/testParam</a><br>
@RequestMapping("/testParam")
public String testParam(String username, String password){
System.out.println("username:"+username+",password:"+password);
return "success";
}
注:
(1)若請求所傳輸?shù)恼埱髤?shù)中有多個同名的請求參數(shù),此時可以在控制器方法的形參中設(shè)置字符串?dāng)?shù)組或者字符串類型的形參接收此請求參數(shù)。
(2)若使用字符串?dāng)?shù)組類型的形參,此參數(shù)的數(shù)組中包含了每一個數(shù)據(jù)。
(3)若使用字符串類型的形參,此參數(shù)的值為每個數(shù)據(jù)中間使用逗號拼接的結(jié)果。
三、注解
1.@RequestParam
@RequestParam是將請求參數(shù)和控制器方法的形參創(chuàng)建映射關(guān)系。
@RequestParam注解一共有三個屬性:
- value:指定為形參賦值的請求參數(shù)的參數(shù)名。
- required:設(shè)置是否必須傳輸此請求參數(shù),默認(rèn)值為true
若設(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。 - defaultValue:不管required屬性值為true或false,當(dāng)value所指定的請求參數(shù)沒有傳輸或傳輸?shù)闹禐?"時,則使用默認(rèn)值為形參賦值。
2.@RequestHeader
@RequestHeader是將請求頭信息和控制器方法的形參創(chuàng)建映射關(guān)系。
@RequestHeader注解一共有三個屬性:value、required、defaultValue,用法同@RequestParam。
3.@CookieValue
@CookieValue是將cookie數(shù)據(jù)和控制器方法的形參創(chuàng)建映射關(guān)系。
@CookieValue注解一共有三個屬性:value、required、defaultValue,用法同@RequestParam。
前面的代碼總和:
<h1>測試請求參數(shù)</h1>
<a th:href="@{/testServletAPI(username='admin',password=123456)}">測試使用servletAPI獲取請求參數(shù)</a><br>
<a th:href="@{/testParam(username='admin',password=123456)}">測試使用控制器的形參獲取請求參數(shù)</a><br>
<form th:action="@{/testParam}" method="get">
用戶名:<input type="text" 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="測試使用控制器的形參獲取請求參數(shù)">
</form>
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
HttpSession session=request.getSession();
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username:"+username+",password:"+password);
return "success";
}
@RequestMapping("/testParam")
public String testParam(@RequestParam(value = "user_name",required = false,defaultValue = "hello") String username,
String password,
String[] hobby, @RequestHeader(value = "sayHaha",required = true,defaultValue = "haha") String host,
@CookieValue("JSESSIONID") String JSESSIONID){
System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobby));
System.out.println("host:"+host);
System.out.println("JSESSIONID:"+JSESSIONID);
return "success";
}
4.通過POJO獲取請求參數(shù)
可以在控制器方法的形參位置設(shè)置一個實體類類型的形參,此時若瀏覽器傳輸?shù)恼埱髤?shù)的參數(shù)名和實體類中的屬性名一致,那么請求參數(shù)就會為此屬性賦值。
<form th:action="@{/testBean}" method="get">
用戶名:<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("/testBean")
public String testBean(User user){
System.out.println(user);
return "success";
}
//最終結(jié)果-->User{id=null, username='張三', password='123', age=23, sex='男',
email='123@qq.com'}
三、解決獲取請求參數(shù)的亂碼問題
解決獲取請求參數(shù)的亂碼問題,可以使用SpringMVC提供的編碼過濾器CharacterEncodingFilter,但是必須在web.xml中進(jìn)行注冊。
<!--配置springMVC的編碼過濾器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filterclass>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中處理編碼的過濾器一定要配置到其他過濾器之前,否則無效。文章來源:http://www.zghlxwxcb.cn/news/detail-661521.html
總結(jié)
以上就是獲取參數(shù)的講解。文章來源地址http://www.zghlxwxcb.cn/news/detail-661521.html
到了這里,關(guān)于SpringMVC之獲取請求參數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!