java – Freemarker和hashmap.如何获取键值
发布时间:2020-12-14 16:36:47 所属栏目:Java 来源:网络整理
导读:我有一个哈希图如下 HashMapString,String map = new HashMapString,String();map.put("one","1");map.put("two","2");map.put("three","3");Map root = new HashMap();root.put("hello",map); 我的Freemarker模板是: htmlbody #list hello?keys as key ${k
|
我有一个哈希图如下
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模板是: <html><body>
<#list hello?keys as key>
${key} = ${hello[key]}
</#list>
</body></html>
目标是在我生成的HTML中显示键值对.请帮我做这个.谢谢! 解决方法
码:
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
模板: <body>
<#list hello?keys as key>
${key} = ${hello[key]}
</#list>
</body>
输出: <body>
two = 2
one = 1
three = 3
</body>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
