Freemarker and hashmap. How do I get key-value

后端 未结 4 1514
Happy的楠姐
Happy的楠姐 2020-12-09 02:55

I have a hash map as below

HashMap map = new HashMap();
map.put(\"one\", \"1\");
map.put(\"two\", \"2\");
map.put         


        
4条回答
  •  时光取名叫无心
    2020-12-09 03:40

    Code:

    HashMap test1 = new HashMap();
    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:

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

    Output:

    
        two = 2 
        one = 1 
        three = 3 
    
    

提交回复
热议问题