Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Enum value using EL with JSTL

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.

like image 433
IaCoder Avatar asked Sep 23 '08 20:09

IaCoder


2 Answers

A simple comparison against string works:

<c:when test="${someModel.status == 'OLD'}">
like image 184
Alexander Vasiljev Avatar answered Nov 04 '22 09:11

Alexander Vasiljev


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>
like image 54
James Avatar answered Nov 04 '22 10:11

James