Java 使用thymeleaf在两个列表上同时迭代 角色 * --
当前它只向我显示一个列表(当前角色),我想显示对象${AllRoles}中绑定的所有角色,并只检查当前指定给特定用户的那些角色 我正在尝试在控制器中的集合中保存角色:Java 使用thymeleaf在两个列表上同时迭代 角色 * --,java,list,spring-boot,iterator,thymeleaf,Java,List,Spring Boot,Iterator,Thymeleaf,当前它只向我显示一个列表(当前角色),我想显示对象${AllRoles}中绑定的所有角色,并只检查当前指定给特定用户的那些角色 我正在尝试在控制器中的集合中保存角色: <div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12">Roles <span class="required">*</span> </l
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Roles
<span class="required">*</span>
</label>
<thbody>
<td><th:block th:each="myRoles : ${MyRoles}">
<input type="checkbox" name="roles"
th:value="${myRoles .id}" checked />
<label th:text="${myRoles .roleName}"></label>
</th:block>--</td>
</thbody>
设置myRolesSet;
myRolesSet=myusr.getRoles();
以下是我在视图中尝试的方式:
Set<UserRole> myRolesSet;
myRolesSet= myusr.getRoles();
您应该像下面的代码示例那样执行此操作: 首先,您需要将所选角色放入控制器方法的映射中,如下所示:
HashMap myRolesMap=newhashmap();
在本例中,我假设您正在为哈希映射使用整数键
其次,您必须遍历AllRoles列表并确定用户是否拥有当前迭代的角色,然后您应该选中复选框。
--
Hi Ahmet,我添加了更新的代码,尝试这样做。但仍然不起作用。您是否按照我在回答中所述(您应该在控制器中执行此操作)将用户角色放置在地图中。我将它们放置在一组中(在控制器端)。不能吗?原因:org.thymeleaf.exceptions.TemplateProcessingException:异常评估SpringEL表达式:“#maps.containsKey(myRolesMap,allRoles.id)”如果我使用hasmap或集合,则会发生此异常
<thbody >
<td><th:block th:each="allRoles : ${allrole}">
<input type="checkbox" name="roles" th:value="${allRoles.id}"
th:checked="${#sets.contains(myRolesSet,allRoles.id)}? 'checked' " />
<label th:text="${allRoles.roleName}"></label>
</th:block></td>
</thbody>
HashMap<Integer, Role> myRolesMap = new HashMap<Integer, Role>();
<thbody>
<td><th:block th:each="role: ${AllRoles}">
<input type="checkbox" name="roles"
th:value="${role.id}" th:checked="${#maps.containsKey(myRolesMap, role.id)}" />
<label th:text="${role.roleName}"></label>
</th:block>--</td>
</thbody>