0

我有以下属性文件:

title = Welcome to Home Page
total = 5
gallery1 = images/gallery/cs.png
text1 =  <b>Counter Strike</b><br />
gallery2 = images/gallery/css.png
text2 =  <b>Counter Strike Source Servers Available</b>
gallery3 = images/gallery/cs.png
text3 =  <b>Counter Strike</b>
gallery4 = images/gallery/cs.png
text4 =  <b>Counter Strike</b>
gallery5 = images/gallery/cs.png
text5 =  <b>Counter Strike</b>

我按如下方式加载它:

public static HashMap<String, String> getPropertyMap(String asPropBundle) throws ApplicationException {
    HashMap<String, String> loMap = new HashMap<String, String>();
    ResourceBundle loRB = (ResourceBundle) moHMProp.get(asPropBundle) ;

    if (loRB == null) {
        throw new ApplicationException("No property bundle loaded with name: " + asPropBundle);
    }

    Enumeration<String> loKeyEnum = loRB.getKeys();

    while (loKeyEnum.hasMoreElements()) {
        String key = (String) loKeyEnum.nextElement();
        loMap.put(key, loRB.getString(key));
    }

    return loMap ;
}

返回的映射设置为 HTTP 请求属性。

我在 JSP 中生成 HTML 如下:

<li class="s3sliderImage">
    <img src="${map.gallery1}" />
    <span>${map.text1}</span>
</li>
.
.
.
<li class="s3sliderImage">
    <img src="${map.gallery2}" />
    <span>${map.text2}</span>
</li>

如何在循环中动态执行此操作?total我有属性文件的属性中的记录总数。

4

1 回答 1

2

资源包已经是一种从键到值的映射,除了它有一个回退机制。为什么要将其内容复制到另一张地图?

只需使用<fmt:message>标签:它的目标正是从资源包中获取消息并将其输出到 JSP 编写器。当然,它可以参数化:

<fmt:setBundle basename="the.base.name.of.your.Bundle"/>
<fmt:message key="text2"/>
<img src="<fmt:message key="gallery2"/>" />

<fmt:message key="greeting">
  <fmt:param value="${user.firstName}"/>
</fmt:message>

最后一段显示“欢迎约翰!” 如果问候键的值为“欢迎{0}!”。

该标签还可以将值存储在变量中,并将 EL 表达式作为参数,因此此代码段应该可以实现您的循环:

<fmt:message var="total" key="total"/>
<c:forEach begin="1" end="${total}" varStatus="loopStatus">
    <li class="s3sliderImage">
        <img src="<fmt:message key="gallery${loopStatus.index}"/>" />
        <span><fmt:message key="text${loopStatus.index}"/></span>
    </li>
</c:forEach>
于 2011-10-15T20:07:59.233 回答