【问题标题】:Using enum in JSTL在 JSTL 中使用枚举
【发布时间】:2011-07-18 21:55:27
【问题描述】:

我正在尝试使用 jstl 进行一些网站开发,但我遇到了 以下问题:

我在这里尝试创建一个下拉列表,其中显示的值为 国家名称,值为国家代码。为此,我 在后端 java 代码中有以下枚举:

public static enum CountryCodes implements EnumConstant {
       USA, CAN, AUS, GBR, DEU, ESP, GUM, IND, ISR, MEX, NZL, PAN, PRI;

       public final String toCountry(){
               switch(this){
               case USA:
                       return "United States";
               case CAN:
                       return "Canada";
               case AUS:
                       return "Australia";
               case GBR:
                       return "Great Britan";
               case DEU:
                       return "Germany";
               case ESP:
                       return "Spain";
               case GUM:
                       return "Guam";
               case IND:
                       return "India";
               case ISR:
                       return "Isreal";
               case MEX:
                       return "Mexico";
               case NZL:
                       return "New Zealand";
               case PAN:
                       return "Panama";
               case PRI:
                       return "Puerto Rico";

               }
               return this.toString();
       }
}

而jsp代码sn-p如下:

<c:set var="countryCodes" value="<%=RequestConstants.CountryCodes.values()%>" />
<td>
    <select id="<%=RequestConstants.CLModifyPage.COUNTRY_CODE%>"
        name="<%=RequestConstants.CLModifyPage.COUNTRY_CODE%>">
        <c:forEach items="${countryCodes}" var="countryCode">
            <c:choose>
                <c:when
                    test="${sessionScope.CURRENT_INSTITUTION.countryCode == countryCode}">
                    <option value="${countryCode}" selected="selected">
                        ${countryCode.toCountry()}</option>
                </c:when>
                <c:otherwise>
                    <option value="${countryCode}">${countryCode.toCountry()}
                        </option>
                </c:otherwise>
            </c:choose>
        </c:forEach>
    </select>
</td>

但是上面的代码有两个问题:

  1. countryCode.toCountry() 真的不起作用...我不确定它应该是什么语法。

  2. 如果 "${sessionScope.CURRENT_INSTITUTION.countryCode}" 不是有效的枚举值,即如果它类似于“AAA”,则比较失败并抛出 java.lang.IllegalArgumentException: no enum const CountryCodes.AAA defined。我该如何解决?

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    你的方法太复杂了。

    重新设计你的枚举如下:

    public enum CountryCode {
    
        USA("United States"), 
        CAN("Canada"),
        AUS("Australia");
        // ...
    
        private String label;
    
        private CountryCode(String label) {
            this.label = label;
        }
    
        public String getLabel() {
            return label;
        }
    
    }
    

    (请注意,它现在有一个完全有价值且更高效的 getter!)

    在 servlet 的 init() 方法或更好的 ServletContextListenercontextInitialized() 方法期间将枚举值存储在应用程序范围内:

    servletContext.setAttribute("countryCodes", CountryCode.values());
    

    最后遍历如下:

    <select name="countryCode">
        <c:forEach items="${countryCodes}" var="countryCode">
            <option value="${countryCode}" ${institution.countryCode == countryCode ? 'selected' : ''}>${countryCode.label}</option>
        </c:forEach>
    </select>
    

    【讨论】:

    • 非常感谢!这个解决方案确实解决了第一个问题。但是当机构.countryCode 不是有效的枚举值时,在比较过程中仍然会出现异常......
    • 它到底怎么可能不是一个有效的枚举值?是CountryCode,对吧?还是毕竟是String?相应地修复类型。否则使用枚举是完全没有意义的。
    • +1,这应该被自动接受,因为自从几年前提出这个问题以来,OP 一直没有出现在 SO 上。我要更改的唯一一点是,我将替换 ${institution.countryCode == countryCode ? 'selected' : ''},而不是 &lt;c:if test="${institution.countryCode == countryCode}"&gt;selected="selected"&lt;/c:if&gt;
    • 是否可以在不向枚举添加get方法的情况下实现这一点?
    【解决方案2】:

    如果你使用的是弹簧,你可以

    <form:select path="_path" >
        <spring:eval expression="T(com.EnumName).values()" var="_enum"/>
        <c:forEach items="${_enum}" var="_value">
            <form:option value="${_value}" label="${_value.label}"/>
        </c:forEach>
    </form:select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-19
      • 2023-03-09
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多