Alex Rivera | Logout

Access Enum value using EL with JSTL

Asked 2008-09-23T20:18:25.687
109

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.

Edit
Report

2 Answers

119

A simple comparison against string works:

<c:when test="${someModel.status == 'OLD'}">
answered 2008-12-15T14:27:13.493
54

If using Spring MVC, the Spring Expression Language (SpEL) can be helpful:

<spring:eval expression="dp.status == T(com.example.Status).VALID" var="isValid" />
<c:if test="${isValid}">
   isValid
</c:if>
answered 2011-05-02T09:57:08.953

Your Answer