Thymeleaf复选框没有将值传递给控制器

时间:2015-10-30 17:58:48

标签: spring-mvc thymeleaf

<form class="form-horizontal" action="#" data-th-action="@{/admin/role/permission/save}" data-th-object="${permission}" method="post">
                    <div class="form-group">
                        <label class="col-sm-5 control-label" data-th-text="#{permission.list.permission.label}">Permission</label>
                        <div class="col-sm-7">
                            <input type="text" hidden="hidden" data-th-value="*{id}" data-th-field="*{id}" ></input>
                            <input type="text" class="form-control" data-th-value="*{permissionname}" data-th-field="*{permissionname}" ></input>
                        </div>
                    </div>
                    <div class="form-group" th:each="role : ${allRoles} ">
                        <label class="col-sm-5 control-label" data-th-text="${role.rolename}">Role 1</label>
                        <div class="col-sm-7">
                            <input type="checkbox" th:field="*{permRoles}" th:value="${role}"></input>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-5 col-sm-7" >
                            <button type="submit" class="btn btn-primary"  name="action"
                                    data-th-value="#{button.action.save}"   data-th-text="#{button.label.save}"  >Save</button>
                            <button type="submit" class="btn btn-default active" name="action"
                                    data-th-value="#{button.action.cancel}" data-th-text="#{button.label.cancel}">Cancel</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-5 col-sm-7" >
                            <p class="text-danger" data-th-if="${#fields.hasErrors('permissionname')}"
                                    data-th-errors="*{permissionname}">type error</p> 
                        </div>
                    </div>
                </form>enter code here

上面是html代码,我有一个权限对象,并希望通过使用复选框为其分配角色列表,将对象传递回控制器。但该值未分配给permission.permroles。

    @RequestMapping(value = "/save", method = RequestMethod.POST)
@PreAuthorize("hasRole('PERMISSION_SAVEORADD')")
public ModelAndView savePermission(@ModelAttribute Permission permission, BindingResult result)
        throws PermissionNotFoundException {
    System.out.println(permission.getPermRoles().size());

    permissionDao.updatePermission(permission);
    return new ModelAndView("redirect:/admin/role/permission/list");
}

以上是我的控制器

请帮助,我被困了几天。

提前谢谢

1 个答案:

答案 0 :(得分:1)

我认为您可以尝试使用带下划线前缀的隐藏输入来解决您的问题。

<form th:action="@{${flowExecutionUrl}(_eventId='change')}" th:method="post">

<input type="hidden" name="_fooBar" th:value="${fooBar}"/>
<input type="checkbox" id="fooBar" name="fooBar" th:checked="${fooBar}" />

<!-- rest of code ommited -->
</form>

我有同样的问题,请参阅我的博文http://lukasgrygar.com/thymeleaf/thymeleaf-tips-and-tricks/#fix-of-problem-with-unchecked-checbox-when-using-thymeleaf-and-spring-web-flow