1.通过控制器方法的实体类类型的形参获取请求参数
要在控制器方法的形参位置设置实体类类型的形参,要保证实体类中的属性的属性名和请求参以通过实体类类型的形参获取请求参数
以下是具体的代码:
这里我们先创建一个User实体类
package com.qcw.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 + '\'' +
'}';
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<form th:action="@{/param/pojo}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录"><br>
</form>
</body>
</html>
package com.qcw.controller;
@Controller
public class TestParamController {
@RequestMapping("/param/pojo")
public String getParamByPojo(User user){
System.out.println("user = " + user);
return "success";
}
}