FreeMarker一对多关系的输出

题记

最近项目中使用到freemarker做模板,遇到刚开始遇到一个问题,一对多的时候list的嵌套问题,一直报错,后来查阅官方文档解决了问题特记录一下
https://freemarker.apache.org/docs/dgui_quickstart_basics.html

正文

环境配置

jdk1.8
eclipse:Version: Oxygen.3a Release (4.7.3a)
freemarker
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.28</version>
</dependency>

两个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<Apple> 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<Apple> getApples() {
        return apples;
    }

    public void setApples(List<Apple> 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

<html>
<head>
  <title>Welcome!</title>
</head>
<body>
<#list fruits as fruit>
  fruitName:${fruit.name}
  fruitDesc:${fruit.desc}
  <ul>
    <#list fruit.apples as apple>
      <li>${apple.name}</li>
      <li>${apple.desc}</li>
      <li>${apple.color}</li>
    </#list>
  </ul>
</#list>
</body>
</html>

测试类

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<String,Object> root = new HashMap<String,Object>();
        List<Fruit> listFruits = new ArrayList<Fruit>();
        Fruit fruit = new Fruit();
        fruit.setName("水果");
        fruit.setDesc("描述");
        List<Apple> list = new ArrayList<Apple>();
        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嵌套的使用,欢迎探讨~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值