Doc Holiday
Doc Holiday

Reputation: 10254

Access Enum value with JSTL?

Okay I got a enum called FiscalMonth:

Oct(1), 
Nov(2), 
Dec(3), 
Jan(4), 
Feb(5), 
Mar(6), 
Apr(7), 
May(8), 
Jun(9), 
Jul(10), 
Aug(11), 
Sep(12);

I need to access the #'s in JSTL...right now..im just manipulating the string as so..

<c:forEach var="month" items="${monthList}" end="11">

<c:choose>
    <c:when test="${fn:substring(month,0,1) == 'O'}">
        "${fn:substring(month,0,1)}${fn:substring(year,2,4)}", "tooltext": "${month} ${year}}"});
    </c:when>
    <c:otherwise>
        "${fn:substring(month,0,1)}", "tooltext": "${month} "});
    </c:otherwise>
</c:choose>

How can I grab the enum #'s?? thanks

Upvotes: 2

Views: 2255

Answers (1)

Nick
Nick

Reputation: 2845

If you can modify the FiscalMonth class, add a method called getNumber() that returns the month number. Then you can access the month number in EL like so: ${month.number}. You could also replace getNumber() and month.number with getWhatever() and month.whatever.

The take-home message is that enumerated types are pretty much classes like any other. They can have methods, instance variables, etc.

Upvotes: 5

Related Questions