在JSTL中使用枚举 [英] Using enum in JSTL

查看:92
本文介绍了在JSTL中使用枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to do some website development using jstl and I run into the following problem:

这里我试图创建一个下拉列表,其中显示的值是
国家/地区名称,值是国家/地区代码。要做到这一点,我
在后端java代码中有以下枚举:

Here I am trying to create a dropdown, where the displayed value is the country names, and the value is the country code. To do this I have the following enum in the backend java code:

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代码片段如下:

And the jsp code snippet is like the following:

<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>

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

But the above code has two problems:


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

  1. countryCode.toCountry() doesn't really work... I am not sure what syntax it should be.

如果$ {sessionScope.CURRENT_INSTITUTION.countryCode}不是有效的枚举值,即if它类似于AAA,然后比较失败并抛出java.lang.IllegalArgumentException:没有定义枚举const CountryCodes.AAA。我怎样才能解决这个问题?

if "${sessionScope.CURRENT_INSTITUTION.countryCode}" is not a valid enum value, i.e, if it's something like "AAA", then the comparison fails and throws an java.lang.IllegalArgumentException: no enum const CountryCodes.AAA defined. How can I get around that?


推荐答案

你的方法太复杂了。

按如下方式重新设计你的枚举:

Redesign your enum as follows:

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()期间将枚举值存储在应用程序范围中方法,或者更好的是,在 <$ c $期间c> ServletContextListener contextInitialized()方法:

Store the enum values in the application scope during servlet's init() method or, better, during ServletContextListener's contextInitialized() method:

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>

这篇关于在JSTL中使用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆