文章目录
- SpringMVC获取请求参数方式
-
- 一、利用servletAPI
- 二、通过控制器方法的形参获取
-
- 1.通过参数直接获取
- [2.@RequestParam 将请求参数和控制器方法的形参创建映射关系](#2.@RequestParam 将请求参数和控制器方法的形参创建映射关系)
- [3.@RequestHeader 将请求头和控制器方法的形参创建映射关系](#3.@RequestHeader 将请求头和控制器方法的形参创建映射关系)
- [4.@CookiValue 将Cookie和控制器方法的形参创建映射关系](#4.@CookiValue 将Cookie和控制器方法的形参创建映射关系)
- 5.通过实体类对象获取请求参数
SpringMVC获取请求参数方式
一、利用servletAPI
1.控制类
java
@RequestMapping("testServletAPI")
//形参request表示当前请求
public String testServletAPI(HttpServletRequest request)
{
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: "+username+" password: "+password);
return "test_param";
}
2.test_param.html
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>测试请求参数</h1>
<a th:href="@{/testServletAPI(username ='admin',password=123456)}">测试请求</a>
</body>
</html>
3.控制台输出
二、通过控制器方法的形参获取
1.通过参数直接获取
1.1.控制器
形参要和请求参数名字相同,这样就可以自动匹配。
java
@RequestMapping("testCon")
public String testControllerParam(String username,String password)
{
System.out.println("username: "+username+" password: "+password);
return "test_param";
}
1.2.控制台输出
1.3.当有重名的参数时
通过get提交方式发现重名的参数会以这种方式传送。
1.3.1.用字符串类型接收
java
@RequestMapping("testCon")
public String testControllerParam(String username,String password,String hobby)
{
System.out.println("username: "+username+" password: "+password+" hobby "+hobby);
return "test_param";
}
当用字符串类型接收时会用,隔开。
1.3.2.用字符串数组接收
java
@RequestMapping("testCon")
public String testControllerParam(String username,String password,String[] hobby)
{
System.out.println("username: "+username+" password: "+password+" hobby "+ Arrays.toString(hobby));
return "test_param";
}
2.@RequestParam 将请求参数和控制器方法的形参创建映射关系
java
@RequestMapping("testCon")
public String testControllerParam(
@RequestParam(value = "username") String username, String password, String[] hobby)
{
System.out.println("username: "+username+" password: "+password+" hobby "+ Arrays.toString(hobby));
return "test_param";
}
3.@RequestHeader 将请求头和控制器方法的形参创建映射关系
java
@RequestMapping("testCon")
public String testControllerParam(@RequestHeader("host") String host)
{
System.out.println("host "+host);
return "test_param";
}
同样具有三个属性
- value
- required
- defaultValue
4.@CookiValue 将Cookie和控制器方法的形参创建映射关系
java
@RequestMapping("testCon")
public String testControllerParam(@CookieValue(value = "j")String cookie)
{
return "test_param";
}
同样具有三个属性
- value
- required
- defaultValue
5.通过实体类对象获取请求参数
5.1.实体类
java
package com.mvc.bean;
public class User {
private Integer id;
private String username;
private String password;
private String sex;
private String email;
public User(Integer id, String username, String password, String sex, String email) {
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
this.email = email;
}
public User() {
}
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 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 + '\'' +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}';
}
}
5.2.表单
html
<form th:action="@{/testBean}" method="post">
用户名<input name="username" type="text"><br>
密码<input type="password" name="password"> <br>
性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女 <br>
邮箱<input type="text" name="email"> <br>
<input type="submit" value="通过实体类对象获取">
</form>
5.3.控制类方法
java
@RequestMapping("testBean")
public String testBean(User user)
{
System.out.println(user);
return "index";
}
5.4控制台输出
控制台输出乱码,在web.xml中设置
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>