Arjun
Arjun

Reputation: 6729

JSP: accessing enum inside JSP EL tags

My java enum looks like this:

public enum EmailType { HOME, WORK, MOBILE, CUSTOMER_SERVICE, OTHER }

In JSP, I am trying to do sth like below, which is not working.

<c:choose>
          <c:when test="${email.type == EmailType.HOME}">(Home)</c:when>
          <c:when test="${email.type == EmailType.WORK}">(Work)</c:when>
</c:choose>

After googling, I found these links: Enum inside a JSP. But, I want to avoid using scriplets in my JSP. How can I access the java enum inside EL tag and do the comparision?? Please help.

Upvotes: 8

Views: 14966

Answers (1)

Andrew
Andrew

Reputation: 94

When an enum is serialized it becomes a string. So just use a string compare.

<c:choose>
          <c:when test="${email.type == 'HOME'}">(Home)</c:when>
          <c:when test="${email.type == 'WORK'}">(Work)</c:when>
</c:choose>

Upvotes: 7

Related Questions