1、通过的ServletAPI获取
只需要在控制器的方法的形参位置设置HTTPRequest request 类型的形参就i可以在控制器方法种使用request对象获取请求参数
@RequestMapping("/servletAPI")
public String getByServletAPI(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";
2、通过控制器方法的形参获取请求参数
前端页面
<form th:action="@{/param}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
@RequestMapping("/param")
public String getParam(String username,String password){
System.out.println("username:"+username+",password:"+password);
return "success";
}
* 只需要在控制器方法的形参位置,设置一个形参,形参的名字和请求参数的名字一致即可
3、通过@RequestParam获取请求参数
@RequestParam是将请求参数和控制器方法的形参创建映射关系
http://localhost:8080/springmvc_rest/param?password=123 |
@RequestMapping("/param")
public String getParam(@RequestParam(value = "username",required = true, defaultValue = "hello")String username, String password){
System.out.println("username:" + username + ",password:" + password);
return "success";
}
@RequestParam注解的属性:
value:设置和形参绑定的请求参数的名字
required:设置是否必须传输value所对应的请求参数
- 默认值为true,表示value所对应的请求参数必须传输否则页面报错 Required String parameter 'xxx' is not present
- 若设置为false,则表示value所对应的请求参数不是必须传输,如果此时获取该参数的值,则为nul
defaultValue:设置当没有传输value所对应的请求参数时,为形参设置的默认值,此时和required属性值无关
4、通过@RequestHeaher获取请求参数
@RequestParam是将请求头信息和控制器方法的形参创建映射关系
@RequestMapping("/param")
public String getParam(String username, String password,
@RequestHeader("referer") String referer){
System.out.println("username = " + username + "," + "password = " + password);
System.out.println("referer = " + referer);
return "success";
}
5、通过@CookieValue获取请求参数
@CookieValue是将cookie数据和控制器方法的形参创建映射关系
@RequestMapping("/param")
public String getParam(String username, String password,
@CookieValue("JSESSIONID") String jsessionId){
System.out.println("username = " + username + "," + "password = " + password);
System.out.println("jsessionId = " + jsessionId);
return "success";
}
返回结果
6、通过pojo获取请求参数
先创建实体类
package com.pojo;
public class User {
private Integer id;
private String username;
private String password;
public User() { }
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
前端页面
<form th:action="@{/param/pojo}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
控制器
@RequestMapping("/param/pojo")
public String getParamByPojo(User user){
System.out.println(user);
return "success";
}