【问题标题】:Thymeleaf checkbox not passing value to controllerThymeleaf 复选框未将值传递给控制器
【发布时间】:2016-01-31 05:52:16
【问题描述】:
<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");
}

上面是我的控制器

请帮忙,我被困了好几天。

提前谢谢你

【问题讨论】:

  • 我知道这是一个很难回答的问题,但真的没人能回答吗?

标签: spring-mvc thymeleaf


【解决方案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

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多