gpt4 book ai didi

java - 如何根据属性文件生成多个 HTML 项目?

转载 作者:行者123 更新时间:2023-12-02 08:08:43 25 4
gpt4 key购买 nike

我有以下属性文件:

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

返回的 map 被设置为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 属性中有记录总数。

最佳答案

资源包已经是一种从键到值的映射,只不过它有一个后备机制。为什么要将其内容复制到另一张 map ?

只需使用 <fmt:message> tag:它的目标正是从资源包中获取消息并将其输出到 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>

最后一个片段显示“欢迎约翰!”如果问候语键的值为“Welcome {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>

关于java - 如何根据属性文件生成多个 HTML 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7778925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com