• freemarker


     

     1、freemarker基本使用

    @Test
        public void freemarker() throws IOException, TemplateException {
            //1、创建一个模板文件
            //2、创建一个Configuration对象
            Configuration configuration = new Configuration(Configuration.getVersion());
            //3、设置模板文件的保存位置
            configuration.setDirectoryForTemplateLoading(new File("F:\workspace\idea\e3\e3-item-web\src\main\webapp\WEB-INF\ftl"));
            //4、模板文件的编码格式
            configuration.setDefaultEncoding("UTF-8");
            //5、加载模板文件,创建一个模板对象
           Template template = configuration.getTemplate("hello.ftl");
            //6、创建一个数据集
            Map<String, Object> data = new HashMap<>();
            data.put("hello", "success");
            Student student = new Student(1, "小明", 18, "回龙观");
            data.put("student", student);
            //7、创建一个Writer对象
            Writer writer = new FileWriter("F:\workspace\freemarker\hello.txt");
            //8、生成静态页面
            template.process(data, writer);
        }

     2、访问pojo属性

    Map<String, Object> data = new HashMap<>();
            Student student = new Student(1, "小明", 18, "回龙观");
            data.put("student", student);
       学生信息:<br>
        学号:${student.id}&nbsp;&nbsp;&nbsp;&nbsp;
        姓名:${student.name}&nbsp;&nbsp;&nbsp;&nbsp;
        年龄:${student.age}&nbsp;&nbsp;&nbsp;&nbsp;
        家庭住址:${student.address}<br>

    3、list集合

    List<Student> stuList = new ArrayList<>();
    stuList.add(new Student(1, "小明1", 18, "成都"));
    stuList.add(new Student(2, "小明2", 19, "达州"));
    data.put("stuList", stuList);
    <table border="1">
            <tr>
                <th>序号</th>
                <th>学号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>家庭住址</th>
            </tr>
            <#list stuList as stu>
            <#if stu_index % 2 == 0>
            <tr bgcolor="red">
            <#else>
            <tr bgcolor="green">
            </#if>
                <td>${stu_index}</td>
                <td>${stu.id}</td>
                <td>${stu.name}</td>
                <td>${stu.age}</td>
                <td>${stu.address}</td>
            </tr>
            </#list>
        </table>

    3、data类型的处理

    可以使用?date,?time,?datetime,?string(parten)&ndash;&gt;
        当前日期:${date?string("yyyy/MM/dd HH:mm:ss")}<br>
        null值的处理:${val!"val的值为null"}<br>
        判断val的值是否为null:<br>
        <#if val??>
        val中有内容
        <#else>
        val的值为null
        </#if>

    4、引入模板

    引用模板测试:<br>
        <#include "hello.ftl">

               

  • 相关阅读:
    使用STM32驱动双通道12位DAC(TLV5618)
    CentOS 7挂载离线yum源
    有关于Git的使用的一点心得和说明
    STM32单片机学习心得——MDK使用技巧
    小米手机连接ADB
    我看操作系统的发展
    centos7下cups + samba共打印服务
    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤
    centos7 更新yum源
    CentOS7 安装Odoo9.0
  • 原文地址:https://www.cnblogs.com/lzb0803/p/9126981.html
Copyright © 2020-2023  润新知