【问题标题】:Thymeleaf checkboxes with objects list from database带有来自数据库的对象列表的 Thymeleaf 复选框
【发布时间】: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


    【解决方案1】:

    标签

    <form:checkboxes items="${rolesList}" path="roles" />
    

    在JSP模板中使用,而Thymeleaf有自己的基于HTML的标签系统(带有Thymeleaf专有的附加标签),默认不使用JSP标签。

    您想要实现的目标可以类似于下面的代码来完成。您必须对其进行更改,使其与您的 Role 课程相匹配。我使用了一个简单的类,其名称为 String 对象,布尔值对应于它的选中角色。

    <ul>
        <li th:each="role : ${rolesList}">
            <label th:for="${role.name}" th:text="${role.name}">Role name</label>
            <input type="checkbox" th:id="${role.name}"
                   th:name="${role.name}" th:checked="${role.active}"/>
        </li>
    </ul>
    

    这是Role 类供参考:

    public class Role {
    
        private String name;
        private boolean active;
    
        public Role(String name, boolean active) {
            this.name = name;
            this.active = active;
        }
    
        public String getName() {
            return name;
        }
    
        public boolean isActive() {
            return active;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-24
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多