Freemarker和hashmap。我如何获得键值

时间:2022-06-30 07:37:58

I have a hash map as below

我有一个哈希映射如下

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);

My Freemarker template is:

我的Freemarker模板是:

<html><body>
    <#list hello?keys as key> 
        ${key} = ${hello[key]} 
    </#list> 
</body></html>

The goal is to display key-value pair in the HTML that I'm generating. Please help me to do it. Thanks!

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

4 个解决方案

#1


38  

Code:

码:

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

Template:

模板:

<body>
<#list hello?keys as key> 
    ${key} = ${hello[key]} 
</#list> 
</body>

Output:

输出:

<body>
    two = 2 
    one = 1 
    three = 3 
</body>

#2


15  

Since 2.3.25, you can do this:

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

<body>
<#list hello as key, value> 
    ${key} = ${value} 
</#list> 
</body>

#3


3  

Use a map that preserves the insertion order of the key-value pairs: LinkedHashMap

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

#4


1  

Before 2.3.25, in case of keys containing objects, you can try to use

在2.3.25之前,如果密钥包含对象,则可以尝试使用

<#assign key_list = map?keys/>
<#assign value_list = map?values/>
<#list key_list as key>
  ...
  <#assign seq_index = key_list?seq_index_of(key) />
  <#assign key_value = value_list[seq_index]/>
  ...
     //Use the ${key_value}
  ...
</#list>

#1


38  

Code:

码:

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

Template:

模板:

<body>
<#list hello?keys as key> 
    ${key} = ${hello[key]} 
</#list> 
</body>

Output:

输出:

<body>
    two = 2 
    one = 1 
    three = 3 
</body>

#2


15  

Since 2.3.25, you can do this:

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

<body>
<#list hello as key, value> 
    ${key} = ${value} 
</#list> 
</body>

#3


3  

Use a map that preserves the insertion order of the key-value pairs: LinkedHashMap

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

#4


1  

Before 2.3.25, in case of keys containing objects, you can try to use

在2.3.25之前,如果密钥包含对象,则可以尝试使用

<#assign key_list = map?keys/>
<#assign value_list = map?values/>
<#list key_list as key>
  ...
  <#assign seq_index = key_list?seq_index_of(key) />
  <#assign key_value = value_list[seq_index]/>
  ...
     //Use the ${key_value}
  ...
</#list>