【发布时间】:2018-01-31 09:29:14
【问题描述】:
我正在努力解决一些 thymeleaf 问题 - 我正在尝试创建用于编辑保存在 mysql db 中的现有用户的表单。我想要实现的是表单中的复选框列表,我可以在其中看到选中的角色(根据 DB 中的信息),并可选择通过选中/取消选中其他角色来更改它们。
查看:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8"></meta>
<title>User edit</title>
</head>
<body>
<form
th:action="@{/edit/{id}/{username}/user(username=${user.username}, id=${user.id})}"
method="post">
<div>
<label> User name : <input type="text" name="username"
placeholder="username" />
</label>
</div>
<div>
<label> User password : <input type="text" name="password"
placeholder="" />
</label>
</div>
<form:checkboxes items="${rolesList}" path="roles" />
<div>
<input type="submit" value="Save" />
</div>
</form>
</body>
</html>
控制器方法:
@GetMapping(path="/edit/{id}/{username}/user")
public String editSpecificUser(Model model, @PathVariable Long id, @PathVariable String username ) {
User user = userRepository.findById(id);
List<Role> rolesList = roleRepository.findAll();
model.addAttribute("user", user);
model.addAttribute("rolesList", rolesList);
return "edituser";
}
用户类别:
@Entity(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
private String username;
private String password;
private int enabled;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
【问题讨论】:
标签: java spring spring-mvc thymeleaf