10

I have a contant list declared in java using enum type, that must appears in a jsp. Java enum declaration :

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>

where as :

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

Works fine. Is there a limitation on enum types ni foreach loop?

jayjaypg22
  • 1,641
  • 5
  • 22
  • 41
  • 2
    Possible duplicate of [iterating over Enum constants in JSP](http://stackoverflow.com/questions/141611/iterating-over-enum-constants-in-jsp) – Stewart Mar 19 '16 at 20:08

5 Answers5

17

Another option is to use a <c:set/> tag like such:

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

Then just iterate over it as such:

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

Your IDE should prompt you to import the YourEnum class.

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
4

Another simple way can be:

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

You need to import these:

<%@ taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="packagename.LIST_TYPE_AFFICHAGE"%>
YROjha
  • 762
  • 1
  • 7
  • 11
4

The values method works fine, my mistake. Indeed the problem was that I didn't put my list in the page scope of my jsp.

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

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

No need of the getNames method

jayjaypg22
  • 1,641
  • 5
  • 22
  • 41
1

You can create a method that returns Enum.values() if you cannot use values directly in your EL expression.

Remove the getNames() from inside your Enum and use a method like this instead somewhere else in your code.

public List<LIST_TYPE_AFFICHAGE> getNames() {
    return new ArrayList<LIST_TYPE_AFFICHAGE>(Arrays.asList(LIST_TYPE_AFFICHAGE.values()));
}
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
0

The EL you are using in the items attribute on c:forEach is trying to call a static method on your enum types. I believe that EL only supports calls on instance methods.

highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91