tags:

views:

10334

answers:

4

I have an Enum called Status defined as such:

public enum Status { 

    VALID("valid"), OLD("old");

    private final String val;

    Status(String val) {
        this.val = val;
    }

    public String getStatus() {
        return val;
    }

}

I would like to access the value of VALID from a JSTL tag. Specifically the test attribute of the <c:when> tag. E.g.

<c:when test="${dp.status eq Status.VALID">

I'm not sure if this is possible, so any help would be much appreciated.

+4  A: 
oneBelizean
+9  A: 

A simple comparison against string works:

<c:when test="${someModel.status == 'OLD'}">
Alexander Vasiljev
For those requiring a source: This is specified (for instance) in section 1.17 of the "Expression Language Specification, version 2.2", which is part of [JSR-245](http://www.jcp.org/en/jsr/detail?id=245).
meriton
+2  A: 

For this purposes I do the following:

<c:set var="abc">
    <%=Status.OLD.getStatus()%>
</c:set>

<c:if test="${someVariable == abc}">
    ....
</c:if>

It's looks ugly, but works!

thanks I've been looking for this ugly thing
phunehehe
A: 

I generally consider it bad practice to mix java code into jsps/tag files. Using 'eq' should do the trick :

<c:if test="${dp.Status eq 'OLD'}">
  ...
</c:if>
Eclatante
So it is a bad practice to use `==` instead of `eq` ? They do both exactly the same, so there's no means of a "trick".
BalusC
Of course, I wasn't making a statement regarding the use of eq vs ==. Many answers to this question involved inserting java code into jsp or tag files which can be a crutch. I favor keeping business logic in java code (where it can be unit tested easily and thoroughly) separate from display logic in the JSP.
Eclatante