zoukankan      html  css  js  c++  java
  • 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">

               

  • 相关阅读:
    day56 js收尾,jQuery前戏
    解决:No module named 'haystack.urls'
    用PicGo+Gitee(码云)搭建Markdown图床
    Python正课138 —— 基础扩展4 django
    Python正课140 —— DRF 进阶1 序列化、增删改查
    Markdown基本语法
    Python正课139 —— DRF 入门1
    用PicGo+GitHub+Typora搭建个人图床
    解决django.core.exceptions.ImproperlyConfiguredmysqlclient 1.3.13 or
    解决:Django中AttributeError:'str'objects has no attribute 'decode'
  • 原文地址:https://www.cnblogs.com/lzb0803/p/9126981.html
Copyright © 2011-2022 走看看