簡體   English   中英

如何使用freemarker模板從哈希圖中獲取特定的鍵值

[英]how to get specific key value from a hashmap using freemarker template

以下是主要功能,其中我將一些鍵值放入哈希圖中。

如何構造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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM