【问题标题】:How to bind an object list to checkbox in thymeleaf?如何将对象列表绑定到百里香中的复选框?
【发布时间】:2016-10-11 05:17:00
【问题描述】:

我在将复选框输入与对象列表绑定时遇到很多困难。问题是当我在输入字段类型复选框上添加 th:field="*{userRole}" 时,我得到了错误的请求作为 Web 浏览器的响应。

这是我的代码:

用户模型类:

public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
    private boolean enabled;
    private List<UserRole> userRole = new ArrayList<UserRole>(0);
}

UserRole 模型类:

public class UserRole implements Serializable {
    @Id
    @GeneratedValue
    private Integer userRoleId;
    @ManyToMany(mappedBy = "userRole")
    private List<User> users;
    @Column(name = "role", nullable = false, length = 45)
    private String role;
}

模型属性:

@ModelAttribute(value = "user")
    public User userModel() {
        return new User();
}

获取方法:

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Map<String, Object> model) {
    List<UserRole> roles = roleService.getAllRoles();
    model.put("roles", roles);
    return "index";
}

我的 POST 方法:

@RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("user") User userModel) {

}

索引页表格:

<form role="form" action="#" th:action="@{/add}" th:object="${user}" method="post">

   <div class="form-group">
            <label for="email">User Name:</label>
            <input type="text" class="form-control" id="username"    th:field="*{username}" required autofocus>
   </div>
   <ul>
        <li th:each="item : ${roles}">
           <input type="checkbox" th:field="*{userRole}" th:value="${item}" />
           <label th:text="${item.role}">Test</label>
        </li>
    </ul>
    <input type="submit" value="Submit" />
</form>

当我点击提交时,浏览器显示为错误请求,但没有 th:field="*{userRole}" 我可以提交表单。有解决此问题的想法吗?

---更新---

问题是 Thymeleaf 无法直接绑定对象。因此添加了新的字符串列表进行绑定。

private List<String> userRoles = new ArrayList<>(0);

然后像@Roel提到的那样更改表格。

<li th:each="item, stat : ${roles}">
    <label th:text="${stat.index}">Test</label>
    <input type="checkbox" th:field="*{userRoles[__${stat.index}__]}" th:value="${item.userRoleId}" />
    <label th:text="${item.role}">Test</label>
</li>

谢谢。

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

    您正在尝试将item 作为值添加到List。这就像试图说ArrayList&lt;Integer&gt; list = 5;

    您需要将项目添加到列表中:

        li th:each="item, stat : ${roles}">
           <input type="checkbox" th:field="*{userRole[__${stat.index}__]}" th:value="${item}" />
           <label th:text="${item.role}">Test</label>
        </li>
    

    我不确定这是否会直接解决您的问题,因为 thymeleaf 存在简单地将对象“添加”到列表中的问题。 Thymeleaf 通常适用于字符串和整数等基本类型。但至少我指出了你的问题所在。试着摆弄一下这个。尝试改用这条线,至少这肯定会起作用:

     <input type="checkbox" th:field="*{userRole[__${stat.index}__].role}" th:value="${item.role}" />
    

    【讨论】:

    • 我试过了,但仍然收到错误请求的响应,控制台上没有错误
    猜你喜欢
    • 2017-01-18
    • 2016-07-29
    • 2019-12-26
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多