您好,我有一个具有某些角色的用户 用户类
public class User {
private Long id;
private String firstName;
private String lastName;
private Set<Role> roles = new HashSet<Role>(0);
public Long getId() { 返回 ID; } public void setId(Long id) { this.id = id; }
public String getFirstName() { return this.firstName; }
public void setFirstName(String firstname) { this.firstName = firstname; }
public String getLastName() { return this.lastName; }
public void setLastName(String lastname) { this.lastName = lastname; }
public Set<Role> getRoles() { return this.roles; }
public void setRoles(Set<Role> roles) { this.roles = roles; }
}
角色.类
public class Role {
private Long id;
private String name;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
在 jsp 文件中,我想进行多项选择,所有角色都具有所选值(用户拥有的角色)。
我试过了:
<select id="roles" name="roles" multiple="true" size="4">
<c:forEach items="${allRoles}" var="role">
<option value="${role.id}" <c:if test="${role.id == roleSelected.id}">selected</c:if> >${role.name}</option>
</c:forEach>
</select>
其中allRoles代表所有角色,roleSelected代表user.roles。 但它不起作用,是否有任何方式可以在 jSTL 中说出类似“if role in user.roles then selected”的内容? 感谢您的任何建议。
更新:
不知何故它不起作用,我在那个类中放了一个记录器我有这个:
public static boolean contains(Collection<?> collection, Object object) {
System.out.println("coll = " + collection.toString());
System.out.println("obj="+ object.toString());
System.out.println("res="+ collection.contains(object));
return collection.contains(object);
}
在日志中我有这个,它应该在第二次测试中结果为真:
coll = [Id :2;Code: TESTName: Temp Manager;Enabled: true;Comment: ;]
obj=Id :1;Code: ADMName: ADMIN;Enabled: true;Comment: For Adminstrators;
res=false
coll = [Id :2;Code: TESTName: Temp Manager;Enabled: true;Comment: ;]
obj=Id :2;Code: TESTName: Temp Manager;Enabled: true;Comment: ;
res=false
coll = [Id :2;Code: TESTName: Temp Manager;Enabled: true;Comment: ;]
obj=Id :3;Code: RHHName: TECHNOMEDIA;Enabled: true;Comment: Dfdf;
res=false
coll = [Id :2;Code: TESTName: Temp Manager;Enabled: true;Comment: ;]
obj=Id :4;Code: RESPONSName: Refd;Enabled: true;Comment: Sdsds;
res=false
最佳答案
我会为此创建一个 EL 函数。
package com.example;
import java.util.Collection;
public final class Functions {
private Functions() {
//
}
public static boolean contains(Collection<Object> collection, Object item) {
return collection.contains(item);
}
}
在 /WEB-INF/functions.tld
中定义它
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<display-name>Custom Functions</display-name>
<tlib-version>1.0</tlib-version>
<uri>http://example.com/functions</uri>
<function>
<name>contains</name>
<function-class>com.example.Functions</function-class>
<function-signature>boolean contains(java.util.Collection, java.lang.Object)</function-signature>
</function>
</taglib>
然后就可以如下使用了
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://example.com/functions" prefix="f" %>
...
<select name="roles" multiple>
<c:forEach items="${allRoles}" var="role">
<option value="${role.id}" ${f:contains(user.roles, role) ? 'selected' : ''}>${role.name}</option>
</c:forEach>
</select>
更新:为了正确比较集合中的对象,您必须相应地实现equals()
和hashCode()
。你好像没有那样做。这是一个按技术 ID 进行比较的基本示例:
public boolean equals(Object other) {
return other instanceof Role && id != null ? id.equals(((Role) other).id) : other == this;
}
public int hashCode() {
return id != null ? getClass().hashCode() + id.hashCode() : super.hashCode();
}
另见:
关于java - 我如何在 jsp/jSTL 中使用选定值进行多选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4059922/