@NoArg
data class User(
var username: String,
var password: String,
// ...
)
// bean
public class User {
private int id;
private String firstname;
private String lastname;
private String username;
private String password;
private String email;
// ...
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstname='" + firstname + ''' +
", lastname='" + lastname + ''' +
", username='" + username + ''' +
", password='" + password + ''' +
", email='" + email + ''' +
'}';
}
}
// controller
@Controller
public class UserController {
@PostMapping("/user")
public String getUser(User user){
System.out.println(user);
return "success";
}
}
<form th:action="@{/user}" method="post">
username:
<label>
<input type="text" name="userName">
</label>
password:
<label>
<input type="password" name="passWord">
</label>
<!-- .... -->
<input type="submit" value="submit">
</form>
form
The name field of the form is consistent with the Java properties will work fine, what if the form
has a lot of content and the fields are inconsistent with the Java bean properties. As shown above.
😊
The above code will work fine.
output:
User{username=a, password=a}
1