Thymeleaf 表单回填 可以使用2种方式:
第一种:直接使用th:value
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" th:href="@{//localhost:8080/css/cc.css}"/>
</head>
<body>
<form>
ID:<input id="id" name="id" th:value="${user.id}"/><br/>
姓名:<input id="name" name="name" th:value="${user.name}"/><br/>
年龄:<input id="age" name="age" th:value="${user.age}"/><br/>
</form>
</body>
</html>
@GetMapping("/one")
public String one(ModelMap mmap){
User user = new User();
user.setId(110);
user.setName("测试");
user.setAge(12);
mmap.addAttribute("user",user);
return "one";
}
第二种:使用 th:object和th:field组合
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" th:href="@{//localhost:8080/css/cc.css}"/>
</head>
<body>
<form th:object="${user}">
ID:<input id="id" name="id" th:field="*{id}"/><br/>
姓名:<input id="name" name="name" th:field="*{name}"/><br/>
年龄:<input id="age" name="age" th:field="*{age}"/><br/>
</form>
</body>
</html>
@GetMapping("/one")
public String one(ModelMap mmap){
User user = new User();
user.setId(110);
user.setName("测试2");
user.setAge(12);
mmap.addAttribute("user",user);
return "one";
}