Access Enum value using EL with JSTL

前端 未结 13 1868
迷失自我
迷失自我 2020-11-29 00:18

I have an Enum called Status defined as such:

public enum Status { 

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

    private final String val;

    Status(String va         


        
相关标签:
13条回答
  • 2020-11-29 00:28

    When using a MVC framework I put the following in my controller.

    request.setAttribute(RequestParameterNamesEnum.INBOX_ACTION.name(), RequestParameterNamesEnum.INBOX_ACTION.name());
    

    This allows me to use the following in my JSP Page.

    <script> var url = 'http://www.nowhere.com/?${INBOX_ACTION}=' + someValue;</script>
    

    It can also be used in your comparison

    <c:when test="${someModel.action == INBOX_ACTION}">
    

    Which I prefer over putting in a string literal.

    0 讨论(0)
  • 2020-11-29 00:30

    You have 3 choices here, none of which is perfect:

    1. You can use a scriptlet in the test attribute:

      <c:when test="<%= dp.getStatus() == Status.VALID %>">

      This uses the enum, but it also uses a scriptlet, which is not the "right way" in JSP 2.0. But most importantly, this doesn't work when you want to add another condition to the same when using ${}. And this means all the variables you want to test have to be declared in a scriptlet, or kept in request, or session (pageContext variable is not available in .tag files).

    2. You can compare against string:

      <c:when test="${dp.status == 'VALID'}">

      This looks clean, but you're introducing a string that duplicates the enum value and cannot be validated by the compiler. So if you remove that value from the enum or rename it, you will not see that this part of code is not accessible anymore. You basically have to do a search/replace through the code each time.

    3. You can add each of the enum values you use into the page context:

      <c:set var="VALID" value="<%=Status.VALID%>"/>

      and then you can do this:

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

    I prefer the last option (3), even though it also uses a scriptlet. This is because it only uses it when you set the value. Later on you can use it in more complex EL expressions, together with other EL conditions. While in option (1) you cannot use a scriptlet and an EL expression in the test attribute of a single when tag.

    0 讨论(0)
  • 2020-11-29 00:30

    So to get my problem fully resolved I needed to do the following:

    <% pageContext.setAttribute("old", Status.OLD); %>
    

    Then I was able to do:

    <c:when test="${someModel.status == old}"/>...</c:when>
    

    which worked as expected.

    0 讨论(0)
  • 2020-11-29 00:34

    I do it this way when there are many points to use...

    public enum Status { 
    
        VALID("valid"), OLD("old");
    
        private final String val;
    
        Status(String val) {
            this.val = val;
        }
    
        public String getStatus() {
            return val;
        }
    
        public static void setRequestAttributes(HttpServletRequest request) {
            Map<String,String> vals = new HashMap<String,String>();
            for (Status val : Status.values()) {
                vals.put(val.name(), val.value);
            }
            request.setAttribute("Status", vals);
        }
    
    }
    

    JSP

    <%@ page import="...Status" %>
    <% Status.setRequestAttributes(request) %>
    
    <c:when test="${dp.status eq Status.VALID}">
    ...
    
    0 讨论(0)
  • 2020-11-29 00:37

    Add a method to the enum like:

    public String getString() {
        return this.name();
    }
    

    For example

    public enum MyEnum {
        VALUE_1,
        VALUE_2;
        public String getString() {
            return this.name();
        }
    }
    

    Then you can use:

    <c:if test="${myObject.myEnumProperty.string eq 'VALUE_2'}">...</c:if>
    
    0 讨论(0)
  • 2020-11-29 00:39

    In Java Class:

        public class EnumTest{
        //Other property link
        private String name;
        ....
    
            public enum Status {
                    ACTIVE,NEWLINK, BROADCASTED, PENDING, CLICKED, VERIFIED, AWARDED, INACTIVE, EXPIRED, DELETED_BY_ADMIN;
                }
    
            private Status statusobj ;
    
        //Getter and Setters
    }
    

    So now POJO and enum obj is created. Now EnumTest you will set in session object using in the servlet or controller class session.setAttribute("enumTest", EnumTest );

    In JSP Page

    <c:if test="${enumTest.statusobj == 'ACTIVE'}">
    
    //TRUE??? THEN PROCESS SOME LOGIC
    
    0 讨论(0)
提交回复
热议问题