一. 通过ServletAPI获取请求参数(一般不用)
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class ParamController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/servletAPI")
public String testServletAPI(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username = " + username + ", password" + password);
return "index";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<form th:action="@{/servletAPI}" method="post">
用户名:<input type="text" name="username"> <br/>
密码:<input type="password" name="password"> <br/>
<input type="submit">
</form>
</body>
</html>
二. 通过控制器形参的方式
1.让形参名与请求的参数名相同
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
@Controller
public class ParamController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/param")
public String testParam(String username, String password, String[] hobby){ //只需要让形参名和请求的参数名相同
System.out.println("username = " + username + ", password" + password + ", hobby = " + Arrays.toString(hobby));
return "index";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<form th:action="@{/param}" method="post">
用户名:<input type="text" name="username"> <br/>
密码:<input type="password" name="password"> <br/>
爱好:<input type="checkbox" name="hobby" value="Java"> Java
<input type="checkbox" name="hobby" value="C++"> C++
<input type="checkbox" name="hobby" value="Python"> Python <br/>
<input type="submit">
</form>
</body>
</html>
2. 使用@RequestParam
使用@RequestParam将形参名和请求参数创建映射关系。
@RequestParam注解一共有三个属性:
①value:指定为形参赋值的请求参数的参数名
②required:设置是否必须传输此请求参数,默认值为true。若设置为true时,则当前请求必须传输value所指定的请求参数,若没有传输该请求参数,且没有设置defaultValue属性,则页面报错400;若设置为false,则当前请求不是必须传输value所指定的请求参数,若没有传输,则注解所标识的形参的值为null
③defaultValue:不管required属性值为true或false,当value所指定的请求参数没有传输或传输的值为" "时,则使用默认值为形参赋值
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Arrays;
@Controller
public class ParamController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/param")
public String testParam(@RequestParam("username") String name, String password, String[] hobby){
System.out.println("username = " + name + ", password" + password + ", hobby = " + Arrays.toString(hobby));
return "index";
}
}
3. @RequestHeader
@RequestHeader是将请求头信息和控制器方法的形参创建映射关系
@RequestHeader注解一共有三个属性:value、required、defaultValue,用法和@RequestParam一样
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Arrays;
@Controller
public class ParamController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/param")
public String testParam(@RequestParam("username") String name,
String password,
String[] hobby,
@RequestHeader("Host") String host){
System.out.println("username = " + name + ", password" + password + ", hobby = " + Arrays.toString(hobby));
System.out.println("host=" + host); //host=localhost:8080
return "index";
}
}
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Arrays;
@Controller
public class ParamController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/param")
public String testParam(@RequestParam("username") String name,
String password,
String[] hobby,
@RequestHeader(value = "hello", defaultValue = "hi") String host){
System.out.println("username = " + name + ", password" + password + ", hobby = " + Arrays.toString(hobby));
System.out.println("host=" + host); //hi
return "index";
}
}
4. @CookieValue
@CookieValue是将cookie数据和控制器方法的形参创建映射关系
@CookieValue注解一共有三个属性:value、required、defaultValue,用法和@RequestParam一样
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
@Controller
public class ParamController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/servletAPI")
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 "index";
}
@RequestMapping("/param")
public String testParam(@RequestParam("username") String name,
String password,
String[] hobby,
@RequestHeader("Host") String host,
@CookieValue("JSESSIONID") String JSESSIONID){
System.out.println("username = " + name + ", password" + password + ", hobby = " + Arrays.toString(hobby));
System.out.println("host=" + host);
System.out.println("JSESSIONID:" + JSESSIONID);
return "index";
}
}
先执行第一个通过Session设置Cookie,然后使用第二个可以获取到Cookie
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<form th:action="@{/servletAPI}" method="post">
用户名:<input type="text" name="username"> <br/>
密码:<input type="password" name="password"> <br/>
<input type="submit">
</form>
<form th:action="@{/param}" method="post">
用户名:<input type="text" name="username"> <br/>
密码:<input type="password" name="password"> <br/>
爱好:<input type="checkbox" name="hobby" value="Java"> Java
<input type="checkbox" name="hobby" value="C++"> C++
<input type="checkbox" name="hobby" value="Python"> Python <br/>
<input type="submit">
</form>
</body>
</html>
三. 通过Bean获取请求参数
实体类属性的名字需要和请求参数的名字相同
package com.mvc.pojo;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
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;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.mvc.controller;
import com.mvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ParamController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/bean")
public String testBean(User user){
System.out.println(user);
return "index";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<form th:action="@{/bean}" method="post">
用户名:<input type="text" name="username"> <br/>
密码:<input type="password" name="password"> <br/>
<input type="submit">
</form>
</body>
</html>
四. 解决获取请求参数乱码问题
在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>