小编Al-*_*ari的帖子

Thymeleaf复选框绑定对象列表

我是Spring的Thymeleaf的新手,我想添加新的Employee内容列表Certificate.我曾经certificateService绑定th:checkbox从控制器传递的列表.我的代码:

调节器

<form action="#" th:action="@{/employee/add}" th:object="${employee}" method="post">
    <table>
        <tr>
            <td>Name</td>
            <td><input type="text" th:field="*{name}"></td>
        </tr>
        <tr>
            <td>Certificate</td>
            <td>
                <th:block th:each="certificate , stat : ${certificates}">
                    <input type="checkbox" th:field="*{certificates}" name="certificates" th:value="${certificate.id]}"/>
                    <label th:text="${certificate.name}" ></label>
                </th:block>
            </td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Add"/></td>
        </tr>
    </table>
</form>
Run Code Online (Sandbox Code Playgroud)

豆子

雇员

@RequestMapping(value = "/add" , method = RequestMethod.GET)
public String add(Model model) {
    model.addAttribute("employee",new Employee());
    model.addAttribute("certificates",certificateService.getList());
    return "add";
}

@RequestMapping(value = "/add" , method = RequestMethod.POST)
public String addSave(@ModelAttribute("employee")Employee …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc thymeleaf

3
推荐指数
1
解决办法
9237
查看次数

标签 统计

spring ×1

spring-mvc ×1

thymeleaf ×1