如何基于属性文件生成多个HTML项? [英] How to generate multiple HTML items based on a properties file?
问题描述
我有以下属性文件:
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>
我正在按如下方式加载它:
I am loading it as follows:
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请求属性.
The returned map is set as HTTP request attribute.
我正在JSP中生成HTML,如下所示:
I am generating the HTML in JSP as follows:
<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
属性中具有记录总数.
How can I do this dynamically in a loop? I have the total amount of records in total
property of the properties file.
推荐答案
资源束已经具有从键到值的映射关系,但它具有后备机制.为什么将其内容复制到另一张地图上?
A Resource Bundle is already sort of a map from keys to values, except it has a fallback mechanism. Why do you copy its content to another map?
只需使用<fmt:message>
标记即可:它的目标正是从资源束中获取一条消息并将其输出到JSP编写器.它可以被参数化,当然:
Just use the <fmt:message>
tag: its goal is precisely to get a message from a resource bundle and to output it to the JSP writer. And it can be parameterized, of course :
<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>
最后一个显示欢迎约翰!"的代码段如果问候语键的值为"Welcome {0}!".
This last snippet displaying "Welcome John!" if the value of the greeting key is "Welcome {0}!".
标签还可以将值存储在变量中,并使用EL表达式作为参数,因此此代码段应该可以实现您的循环:
The tag can also store the value in a variable, and take an EL expression as parameter, so this snippet should work to implement your loop:
<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>
这篇关于如何基于属性文件生成多个HTML项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!