如何将对象列表绑定到百万美元的复选框? [英] How to bind an object list to checkbox in thymeleaf?

查看:143
本文介绍了如何将对象列表绑定到百万美元的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a lot of difficulty with binding checkbox inputs with a object list.Issue is when I add th:field="*{userRole}" on input field type checkbox then I am getting Bad request as response on web browser.

这是我的代码:

用户模型类:

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 Model class:

UserRole Model class:

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();
}

GET方法:

@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}我可以提交表单。有什么想法解决这个问题?

when I click submit, browser shows as a Bad request, But without th:field="*{userRole}" I can submit the form. Any idea to resolve this issue?

---更新---

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

issue was Thymeleaf unable to bind object directly. So added new List of String for binding.

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

然后将表格更改为@Roel提及。

and then changed the form as @Roel mentioned.

<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>

谢谢。

推荐答案

您正在尝试将 item 作为值添加到列表。这就像试图说 ArrayList< Integer> list = 5;

You are trying to add item as the value to a List. It's like trying to say ArrayList<Integer> list = 5;

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

What you need is to add the item to the list:

    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通常使用字符串和整数等基本类型。但至少我指出了你的问题所在。尝试一下这一点。请尝试使用此行,至少这肯定会有效:

I'm unsure if this will directly solve your problem as thymeleaf has issues with simply "adding" an object to a list. Thymeleaf generally works with basic types like strings and integers. But at least I pointed out where your problem lies. Try fiffling around with this a little bit. Try using this line instead, at least this will definitely work:

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

这篇关于如何将对象列表绑定到百万美元的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆