如何在具有选定值的jsp / jstl中进行多选? [英] How can i do a multiselect in jsp/jstl with selected value?
问题描述
您好我有一个角色的用户
User.class
Hello I have an User with some Roles User.class
public class User {
private Long id;
private String firstName;
private String lastName;
private Set<Role> roles = new HashSet<Role>(0);
public Long getId(){return id; }
public void setId(Long id){this.id = id; }
public Long getId() { return 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; }
}
Role.class
Role.class
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; }
}
我想进行多重选择将具有所选值的所有角色(用户拥有的角色)。
in the jsp file i want to make a multiple select that will have all the roles with the selected values (roles that the user have).
我尝试过:
<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中说如果在user.roles中的角色然后被选中?
感谢任何建议。
where allRoles represent all the role, roleSelected represent the user.roles. but it not working, is there any manner to say something in jstl like " if role in user.roles then selected " ? thank for any advise.
更新:
不知何故它不起作用,我把一个记录器放在那个类中我有这个:
somehow its not working, i put a logger in that class i have this :
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);
}
在日志中我有这个,它应该在第二次测试中得到结果:
in the log i have this ,it should result true in the second test:
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函数。
I'd create an EL function for that.
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);
}
}
在<$ c $中定义c> /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>
然后您可以按如下方式使用
Then you can use it as follows
<%@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>
更新:in为了正确地比较集合中的对象,你必须相应地实现 equals()
和 hashCode()
。你似乎没有这样做。这是一个基本的例子,按技术ID进行比较:
Update: in order to properly compare objects in a collection you have to implement equals()
and hashCode()
accordingly. You seem not have done that. Here's a basic example which compares by technical 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();
}
另请参阅:
- HashSet无法识别相同的对象
- 了解equals和hashcode
- 覆盖equals和hashcode
- 实现equals和hashcode的正确方法
- HashSet does not recognize same objects
- Understanding equals and hashcode
- Overriding equals and hashcode
- Right way to implement equals and hashcode
See also:
这篇关于如何在具有选定值的jsp / jstl中进行多选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!