Freemarker和哈希图。我如何获得键值

【字号: 日期:2024-02-23浏览:45作者:雯心
如何解决Freemarker和哈希图。我如何获得键值?

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 configurationTemplate template = cfg.getTemplate('test.ftl'); // Filename of your templateStringWriter sw = new StringWriter(); // So you can use the output as Stringtemplate.process(root, sw); // process the template to outputSystem.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>解决方法

我有一个哈希图如下

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中显示键值对。请帮我做。谢谢!

相关文章: