【发布时间】:2019-09-17 13:28:36
【问题描述】:
我有一个用 Thymeleaf + Spring 构建的典型 updateUser 表单。我可以列出用户角色,但我试图将它们放入选择框组件中。我正在为这条线苦苦挣扎:selected="${user.hasRole(role.role)}"。现在,我知道这个组件可以工作,这只是访问一个布尔函数来启用它的问题。我正在尝试使用 select th:object 引用表单对象的函数。我的语法不起作用。我也尝试访问该函数,就像以前的输入标签一样(仅使用没有对象本身的名称。即用户名或在本例中为 hasRole()。这也不起作用。
<form th:object="${user}"
th:action="@{'/admin/usermanagement/adduser'} " method="post">
<div class="row">
<div class="col-md-3 form-group">
<label>Username:</label> <input type="text" class="form-control"
th:field="*{username}" />
</div>
</div>
<div class="col-md-3">
<h5>Roles :</h5>
</div>
<select class="js-example-basic-multiple" style="width: 75%"
name="froles" id="froles" multiple="multiple">
<option th:each="role : ${roles}" th:value="${role.role}"
th:selected="${user.hasRole(role.role)}"
th:text="${role.role}"></option>
</select>
<button type="submit" class="btn btn-primary">Submit</button>
【问题讨论】: