2010-10-20 19 views
8

Ho una lista contant dichiarata in java usando il tipo enum, che deve apparire in un jsp. Java dichiarazione enum:JSTL foreach on enum

public class ConstanteADMD { 


    public enum LIST_TYPE_AFFICHAGE { 
     QSDF("qsmldfkj"), POUR("azeproui"); 

     private final String name; 

     @Override 
     public String toString() { 
      return name; 
     } 

     private LIST_TYPE_AFFICHAGE(String name) { 
      this.name = name; 
     } 

     public static List<String> getNames() { 
      List<String> list = new ArrayList<String>(); 
      for (LIST_TYPE_AFFICHAGE test : LIST_TYPE_AFFICHAGE.values()) { 
       list.add(test.toString()); 
      } 
      return list; 
     } 
    } 
} 


<select name="typeAffichage" id="typeAffichage"> 
    <c:forEach var="type" items="${netcsss.outils.ConstanteADMD.LIST_TYPE_AFFICHAGE.names}"> 
     <option value="${type}">${type}</option> 
    </c:forEach> 
</select> 

dove come:

<select name="typeAffichage" id="typeAffichage"> 
    <c:choose> 
     <c:when test="${catDecla ne null}"> 
      <option 
       value="<%=catDecla.getCatDecla().getSTypeAffichage()%>" 
       selected="selected"><%=catDecla.getCatDecla().getSTypeAffichage()%></option> 
     </c:when> 
    </c:choose> 
     <%List<String> list = ConstanteADMD.LIST_TYPE_AFFICHAGE.getNames(); 
       for(String test : list) { 
      %> 
     <option value="<%=test%>"><%=test%></option> 
     <%}%> 
</select> 

funziona bene. Esiste una limitazione per enum types ni foreach loop?

+2

Eventuali duplicati di [iterare su costanti Enum in JSP] (http: // StackOverflow .com/questions/141611/iterating-over-enum-constants-in-jsp) – Stewart

risposta

3

Il metodo valori funziona bene, il mio errore. In effetti il ​​problema era che non ho inserito la mia lista nello scope della pagina del mio jsp.

<% pageContext.setAttribute("monEnum", ConstanteADMD.ListTypeAffichage.values()); %> 

... 
<c:forEach var="entry" items="${monEnum}"> 
    <option>${entry.type}</option> 
</c:forEach> 

Non c'è bisogno del metodo getNames

0

EL che si sta utilizzando nell'attributo items in c: forEach sta tentando di chiamare un metodo statico sui tipi enum. Credo che EL supporti solo le chiamate sui metodi di istanza.

1

È possibile creare un metodo che restituisce Enum.values() se non è possibile utilizzare i valori direttamente nell'espressione EL.

Rimuovere il getNames() dall'interno dell'Enum e utilizzare un metodo come questo invece da qualche altra parte nel codice.

public List<LIST_TYPE_AFFICHAGE> getNames() { 
    return new ArrayList<LIST_TYPE_AFFICHAGE>(Arrays.asList(LIST_TYPE_AFFICHAGE.values())); 
} 
14

Un'altra opzione è quella di utilizzare un tag <c:set/> come tale:

<c:set var="enumValues" value="<%=YourEnum.values()%>"/> 

Poi basta scorrere su di esso come tale:

<c:forEach items="${enumValues}" var="enumValue"> 
    ... 
</c:forEach> 

L'IDE dovrebbe richiedere di importare la classe YourEnum.

+0

Non ho avuto bisogno di fare xer21

2

Un altro modo semplice può essere:

<c:forEach items="<%=LIST_TYPE_AFFICHAGE.values()%>" var="entry"> 
    <option>${entry.name }</option> 
</c:forEach> 

è necessario importare questi:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@page import="packagename.LIST_TYPE_AFFICHAGE"%>