Spring MVC入门(三):获取请求参数
- 通过ServletAPI获取
@RequestMapping("/testServletAPI")
//形参位置的request表示当前请求
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";
}
# http://localhost:8080/testServletAPI?username=goudan&password=12345
- 通过形参获取请求参数
@RequestMapping("/testParam")
public String testParam(String password,String hobby){
System.out.println(password:"+password+",hobby:"+ hobby);
return "success";
}
# http://localhost:8080/testParam?password=12345&hobby=a&hobby=b
# 控制台:password:12345,hobby:a,b
@RequestMapping("/testParam")
public String testParam(String password,String[] hobby){
System.out.println(password:"+password+",hobby:"+ Arrays.toString(hobby));
return "success";
}
# http://localhost:8080/testParam?password=12345&hobby=a&hobby=b
# 控制台:password:12345,hobby:[a,b]
- @RequestParam
@RequestParam是将请求参数和控制器方法的形参创建映射关系
@RequestParam注解一共有三个属性:
value:指定为形参赋值的请求参数的参数名
required:设置是否必须传输此请求参数,默认值为true
若设置为true时,则当前请求必须传输value所指定的请求参数,若没有传输该请求参数,且没有设置
defaultValue属性,则页面报错400:Required String parameter 'xxx' is not present;若设置为
false,则当前请求不是必须传输value所指定的请求参数,若没有传输,则注解所标识的形参的值为
null
defaultValue:不管required属性值为true或false,当value所指定的请求参数没有传输或传输的值
为""时,则使用默认值为形参赋值
- @RequestHeader
@RequestHeader是将请求头信息和控制器方法的形参创建映射关系
@RequestHeader注解一共有三个属性:value、required、defaultValue,用法同@RequestParam
- @CookieValue
@CookieValue是将cookie数据和控制器方法的形参创建映射关系
@CookieValue注解一共有三个属性:value、required、defaultValue,用法同@RequestParam
- 测试案例
@RequestMapping("/testParam")
public String testParam(
@RequestParam(value = "user_name", required = false, defaultValue = "hehe") 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";
}
- 通过POJO获取请求参数
# 注意,前端为form表单
<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";
}
-
get请求的乱码是由tomcat造成的,解决get请求乱码,打开tomcat配置文件
-
解决post请求的乱码,DispatcherServlet获取请求参数,所以应该在DispatcherServlet获取之前设置字符集(启动顺序:监听器、过滤器、servlet)
# web.xml中配置如下
<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>