8

I have an enum class USState. I would like to iterate through the states in a JSP.

Is it possible to access a list of USStates without first setting such a list as an attribute? It seems that something as static as an enum should always be available, but I can't figure out how to do it.

Here's what I'm looking for: (except working)

<c:forEach var="state" items="${USState.values}" >
    <option value="${state}">${state}</option>
</c:forEach>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
  • http://stackoverflow.com/questions/2237135/how-can-i-produce-a-select-tag-using-jstl-or-standard-actions-in-a-jsp Shouldn't you be grabbing Struts? :) – BalusC Feb 10 '10 at 22:50
  • I think that's what I'll do. I'm building something small, and the initial thought was that Struts wouldn't be necessary. – Eric Wilson Feb 11 '10 at 00:45

3 Answers3

9

You will have to create a list somewhere on your backing code and pass it as a model parameter. Preferably in an ServletContextListener (as advised by BalusC) and put it in the ServletContext (i.e. application scope):

servletContext.setAttribute("statesList", YourEnum.values());
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 4
    As it's already a constant I'd just put it in application scope, if necessary with help of `ServletContextListener`. Another alternative is a custom EL resolver or EL function, but that's a bit of work. – BalusC Feb 10 '10 at 22:33
  • @BalusC: I've started to do this already. I started to think I must be hacking around the correct approach. – Eric Wilson Feb 11 '10 at 00:44
  • 4
    I'm pretty sure that there is no need for the `Arrays.asList()` call. You can just do `servletContext.setAttribute("statesList", YourEnum.values());` – Matt Ball Nov 01 '10 at 20:02
  • @Matt Ball - agreed. Fixing it. – Bozho Nov 01 '10 at 20:03
  • Anyway, +1 for application scoping with a `ServletContextListener`. – Matt Ball Nov 01 '10 at 20:30
9

You can also consider to wrap it in a Javabean like follows:

package com.stackoverflow.q2240722;

public class StateBean {

    public State[] getValues() {
        return State.values();
    }

}

This way it's accessible by <jsp:useBean>:

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

<jsp:useBean id="stateBean" class="com.stackoverflow.q2240722.StateBean" />

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2240722</title>
    </head>
    <body>
        <select>
            <c:forEach items="${stateBean.values}" var="state">
                <option value="${state}">${state}</option>        
            </c:forEach>
        </select>
    </body>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
7

Note that you can also use a scriptlet (I don't think it's too harmful in such a simple case):

<c:forEach var="state" items="<%= USState.values() %>" >

(USState should be either fully qualified or imported using <%@ page import = "..." %>

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • This is the current state of my code. It works, but I thought I must be missing the easier way. – Eric Wilson Feb 11 '10 at 00:41
  • 2
    My personal opinion is that this is more maintainable than the other solutions. It's short and you don't have to rely on strings that can change and break without you noticing. In this instance I won't run away from scriptlets. – Adrian Lopez Jan 31 '14 at 09:52