如何使用freemarker模板从hashmap中获取特定的键值下面的



是我将一些键值放入哈希图的主要函数。

如何构造freemarker模板,使其只读取一个键值,而不遍历整个列表

import freemarker.template.TemplateException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class main {
public static void main(String[] args) throws  TemplateException {
HashMap<String, String> gtmData = new HashMap<String, String>();
Map gtm = new HashMap();
gtmData.put("Uid", "a12");
gtmData.put("SettlementCurrency","USD");
gtmData.put("Quantity","123455");
Map root = new HashMap();
root.put("hello", gtmData);
FreeMarkerConverter convert = new FreeMarkerConverter();
try {
convert.setTemplateName("gtm-temp-h");
convert.convert(root);
}
catch(IOException e) {
e.printStackTrace();
}
}
}

下面是freemarker模板,它代替了root。SettlementCurrency我想要那个键的值,但每个例子都显示打印整个列表

"header"[
{
messageid :$(root.uid)
SettlementCurrency:$(root.SettlementCurrency)
}

]

我没有完全遵循您的示例,但在我的FreeMarker模板中,我使用了Map[key]的表示法。

创建地图

Map<String, String> ringImageLocationMap = new HashMap<>();
for (Ring ringItem : ringItems){
String url = getRingImageUrl(ringItem);
ringImageLocationMap.put(ringItem.getIoItemGuid(), url);
}

Java向模型添加Map:

model.put("ringImageLocationMap", orderSummary.getRingImageLocationMap());

Freemarker访问地图

<#if (ringImageLocationMap[item.ioItemGuid])??> 
<#assign location = ringImageLocationMap[item.ioItemGuid]>                                              
<td colspan="4" style="padding-top: 100px; padding-bottom: 130px;">
<img id="productImage" src="${location}" width="259"/>
</td>
<#else>
<td colspan="4" style="padding-top: 100px; padding-bottom: 130px;">
&nbsp;
</td>
</#if>

最新更新