English 中文(简体)
如何在具有选定价值的jsp/jstl中进行多功能?
原标题:How can i do a multiselect in jsp/jstl with selected value?

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 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

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>

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.


<>Update:

some its

 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);
    }

}

在<代码>上界定。

<?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>

<>Update:为了在收集中适当比较物体,您必须执行qualitys(<>/code>和hashCode()。 你似乎没有这样做。 下面是按技术识别法比较的一个基本例子:

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();
}

See also:

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...