首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Freemarker和hashmap。如何获取key-value

Freemarker和hashmap。如何获取key-value
EN

Stack Overflow用户
提问于 2013-02-12 05:38:32
回答 4查看 72.3K关注 0票数 24

我有一个哈希图,如下所示

代码语言:javascript
复制
HashMap<String, String> map = new HashMap<String, String>();
map.put("one", "1");
map.put("two", "2");
map.put("three", "3");

Map root = new HashMap();
root.put("hello", map);

我的Freemarker模板是:

代码语言:javascript
复制
<html><body>
    <#list hello?keys as key> 
        ${key} = ${hello[key]} 
    </#list> 
</body></html>

这样做的目的是在我生成的HTML中显示键值对。请帮我做这件事。谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-12 05:49:33

代码:

代码语言:javascript
复制
HashMap<String, String> test1 = new HashMap<String, String>();
Map root = new HashMap();
test1.put("one", "1");
test1.put("two", "2");
test1.put("three", "3");
root.put("hello", test1);


Configuration cfg = new Configuration(); // Create configuration
Template template = cfg.getTemplate("test.ftl"); // Filename of your template

StringWriter sw = new StringWriter(); // So you can use the output as String
template.process(root, sw); // process the template to output

System.out.println(sw); // eg. output your result

模板:

代码语言:javascript
复制
<body>
<#list hello?keys as key> 
    ${key} = ${hello[key]} 
</#list> 
</body>

输出:

代码语言:javascript
复制
<body>
    two = 2 
    one = 1 
    three = 3 
</body>
票数 47
EN

Stack Overflow用户

发布于 2016-07-09 02:46:04

从2.3.25开始,您可以这样做:

代码语言:javascript
复制
<body>
<#list hello as key, value> 
    ${key} = ${value} 
</#list> 
</body>
票数 23
EN

Stack Overflow用户

发布于 2014-03-06 21:24:13

使用保持键-值对插入顺序的映射: LinkedHashMap

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14821329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档