环境配置
jdk1.8 eclipse:Version: Oxygen.3a Release (4.7.3a) freemarkerorg.freemarker freemarker 2.3.28
两个model类
Fruit类
package com.test.freemarker;
import java.util.List;
/**
* Fruit
*
* @author relieved-gao
* @version 1.0
* @date 2018-06-20 15:10
*/
public class Fruit {
private String name;
private String desc;
private List apples;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public List getApples() {
return apples;
}
public void setApples(List apples) {
this.apples = apples;
}
}
Apple类
package com.test.freemarker;
/**
* Fruit
*
* @author relieved-gao
* @version 1.0
* @date 2018-06-20 15:10
*/
public class Apple {
private String name;
private String desc;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
模板位置放在D盘根目录,可以自己在测试类里面修改
test.ftl
- <#list fruit.apples as apple>
- ${apple.name}
- ${apple.desc}
- ${apple.color}
测试类
package com.test.freemarker;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
public class Test {
public static void main(String[] args) throws Exception {
/* ------------------------------------------------------------------------ */
/* You should do this ONLY ONCE in the whole application life-cycle: */
/* Create and adjust the configuration singleton */
Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);
cfg.setDirectoryForTemplateLoading(new File("D://"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
/* ------------------------------------------------------------------------ */
/* You usually do these for MULTIPLE TIMES in the application life-cycle: */
/* Create a data-model */
Map root = new HashMap();
List listFruits = new ArrayList();
Fruit fruit = new Fruit();
fruit.setName("水果");
fruit.setDesc("描述");
List list = new ArrayList();
Apple a1 = new Apple();
a1.setColor("red");
a1.setName("红苹果");
a1.setDesc("红苹果描述");
Apple a2 = new Apple();
a2.setColor("tsing");
a2.setName("青苹果");
a2.setDesc("青苹果描述");
list.add(a1);
list.add(a2);
fruit.setApples(list);
listFruits.add(fruit);
root.put("fruits",listFruits);
/* Get the template (uses cache internally) */
Template temp = cfg.getTemplate("test.ftl");
/* Merge data-model with template */
Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);
// Note: Depending on what `out` is, you may need to call `out.close()`.
// This is usually the case for file output, but not for servlet output.
}
}
输出结果

以上便是一对多的list嵌套的使用