如何为jsp中的下拉列表提供字符串常量 [英] how to provide string constants for drop down lists in jsp

查看:64
本文介绍了如何为jsp中的下拉列表提供字符串常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个jsp页面中,我向客户显示下拉列表以选择信用卡类型,信用卡的到期月份,到期年份. 我正在寻找可以放置此必要字符串的方法,而不是在html中对其进行硬编码.

In a jsp page ,I am presenting the customer with drop down lists to select creditcardtype,expiration month,expiration year of credit card. I am looking at ways the necessary strings for this can be put ,other than hardcoding them in html.

谢谢

标记

<tr>
<td>
    <select id="creditCardType" title="select card type" name="creditCardType">
        <option value="M0">MasterCard</option>
        <option value="D0">Discover</option>
        <option value="J0">JCB</option>
        <option value="I0">Diners Club</option>
        <option value="A0">American Express</option>
        <option value="V0">Visa</option>
        <option value="V">Amazon.com Visa</option>
        <option value="G21">Amazon.com Store Card</option>
    </select>
</td>
</tr>
<tr>
<td>Expiration Date</td>
<td> 
                     <select id="cardexpiryMonth" name="cardexpiryMonth">
                        <option value="01" selected="selected">01</option>
                        <option value="02" >02</option>
                        <option value="03" >03</option>
                        <option value="04" >04</option>
                        <option value="05" >05</option>
                        <option value="06" >06</option>
                        <option value="07" >07</option>
                        <option value="08" >08</option>
                        <option value="09" >09</option>
                        <option value="10" >10</option>
                        <option value="11" >11</option>
                        <option value="12" >12</option>
                      </select>
</td>
<td>
                    <select id="cardexpiryYear" name="cardexpiryYear">
                        <option value="2011" >2011</option>
                        <option value="2012" selected="selected">2012</option>
                        <option value="2013" >2013</option>
                        <option value="2014" >2014</option>
                        <option value="2015" >2015</option>
                        <option value="2016" >2016</option>
                        <option value="2017" >2017</option>
                        <option value="2018" >2018</option>
                        <option value="2019" >2019</option>
                        <option value="2020" >2020</option>
                        <option value="2021" >2021</option>
                        <option value="2022" >2022</option>
                        <option value="2023" >2023</option>
                        <option value="2024" >2024</option>
                        <option value="2025" >2025</option>
                        <option value="2026" >2026</option>

                        <option value="2027" >2027</option>
                        <option value="2028" >2028</option>
                        <option value="2029" >2029</option>
                        <option value="2030" >2030</option>
                        <option value="2031" >2031</option>
                      </select>
</td>
</tr>

推荐答案

如果它是应用程序范围的常量,只需在应用程序启动期间将它们放在应用程序范围内即可.应用范围由对象表示,该对象是ServletContext的属性.另请参见 servlet如何工作?实例化,会话,共享变量和多线程

If it are application wide constants, just put them in the application scope during application startup. The application scope is represented by the object being an attribute of ServletContext. See also How do servlets work? Instantiation, sessions, shared variables and multithreading

如果CDI恰好在您的环境中可用(即您正在运行普通的JEE服务器,例如WildFly,Payara,TomEE等),则只需使用@ApplicationScoped bean代替ServletContextListener.

If CDI happens to be available in your environment (i.e. you're running a normal JEE server such as WildFly, Payara, TomEE, etc), then just use an @ApplicationScoped bean instead of a ServletContextListener.

@Named @ApplicationScoped
public class Data {

    private Map<String, String> creditCardTypes;

    @PostConstruct
    public void init() { 
        creditCardTypes = new LinkedHashMap<String, String>();
        creditCardTypes.put("M0", "MasterCard");
        creditCardTypes.put("D0", "Discover");
        // ...
    }

    public Map<String, String> getCreditCardTypes() {
        return creditCardTypes;
    }
}

(请注意,我使用LinkedHashMap是因为它保持与HashMap相反的插入顺序)

(note that I used LinkedHashMap as it maintains insertion order in contrary to HashMap)

这样,EL在任何JSP中都可以将其用作${data.creditCardTypes}.然后,您可以使用 JSTL <c:forEach>对其进行迭代.它还支持对Map进行迭代,并且每次迭代都将给出

This way it's available as ${data.creditCardTypes} by EL in any JSP. You can then use JSTL <c:forEach> to iterate over it. It also supports iterating over a Map and each iteration will give a Map.Entry back which in turn has getKey() and getValue() methods which are accessible in EL as well.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<select id="creditCardType" title="select card type" name="creditCardType">
    <c:forEach items="${data.creditCardTypes}" var="creditCardType">
        <option value="${creditCardType.key}">${creditCardType.value}</option>
    </c:forEach>
</select>

没有可用的CDI吗?

如果CDI不可用(即您没有运行普通的JEE服务器,例如Tomcat,Jetty,Undertow等,并且您不想

No CDI available?

If CDI is not available (i.e. you're not running a normal JEE server, such as Tomcat, Jetty, Undertow, etc, and you don't want to install CDI for some reason), then you can use the init() method of an arbitrary servlet or, better, a ServletContextListener.

@WebListener
public class Data implements ServletContextListener {

    private Map<String, String> creditCardTypes;

    @Override
    public void contextInitialized(ServletContextEvent event) { 
        creditCardTypes = new LinkedHashMap<String, String>();
        creditCardTypes.put("M0", "MasterCard");
        creditCardTypes.put("D0", "Discover");
        // ...

        event.getServletContext().setAttribute("data", this);
    }

    public Map<String, String> getCreditCardTypes() {
        return creditCardTypes;
    }
}

通过这种方式,EL在任何JSP中也可以作为${data.creditCardTypes}使用.

This way it's also available as ${data.creditCardTypes} by EL in any JSP.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<select id="creditCardType" title="select card type" name="creditCardType">
    <c:forEach items="${data.creditCardTypes}" var="creditCardType">
        <option value="${creditCardType.key}">${creditCardType.value}</option>
    </c:forEach>
</select>

这篇关于如何为jsp中的下拉列表提供字符串常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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