Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP: accessing enum inside JSP EL tags

Tags:

jsp

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.

like image 401
Arjun Avatar asked Mar 03 '11 15:03

Arjun


1 Answers

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>
like image 129
Andrew Avatar answered Nov 22 '22 14:11

Andrew