zoukankan      html  css  js  c++  java
  • freemarker页面静态化

    1、工程结构

    2、

      Student

    public class Student {
        private int id;
        private String name;
        private String address;
    
        public Student() {}
        public Student(int id, String name, String address) {
            super();
            this.id = id;
            this.name = name;
            this.address = address;
        }
        // getter和setter方法省略
    }

      StudentExt

    public class StudentExt {
        private String title;
        private Student student;
    
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public Student getStudent() {
            return student;
        }
        public void setStudent(Student student) {
            this.student = student;
        }
    }

      FreeMarkerTest

    package com.test;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.junit.Test;
    
    import com.pojo.Student;
    import com.pojo.StudentExt;
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    
    public class FreeMarkerTest {
        @Test
        public void testFreeMarker() throws Exception {
            Template template = getTemplate("first.ftl");
            
            // 第七步:创建模板需要的数据集。可以是一个map对象也可以是一个pojo,把模板需要的数据都放入数据集。
            Map<String, Object> root = new HashMap<>();
            root.put("hello", "hello freemarker");
            // 第八步:创建一个Writer对象,指定生成的文件保存的路径及文件名。
            Writer out = new FileWriter(new File("D:\temp\html\first.html"));
            // 第九步:调用模板对象的process方法生成静态文件。需要两个参数数据集和writer对象。
            template.process(root, out);
            // 第十步:关闭writer对象。
            out.flush();
            out.close();
        }
    
        @Test
        public void testFreeMarker2() throws Exception {
            Template template = getTemplate("second.ftl");
            
            // 第七步:创建模板需要的数据集。可以是一个map对象也可以是一个pojo,把模板需要的数据都放入数据集。
            StudentExt s = new StudentExt();
            s.setTitle("生成静态网页");
            Student stu = new Student();
            stu.setId(1);
            stu.setName("张三");
            stu.setAddress("深圳南山软件园");
            s.setStudent(stu);
            
            // 第八步:创建一个Writer对象,指定生成的文件保存的路径及文件名。
            Writer out = new FileWriter(new File("D:\temp\html\second.html"));
            // 第九步:调用模板对象的process方法生成静态文件。需要两个参数数据集和writer对象。
            template.process(s, out);
            // 第十步:关闭writer对象。
            out.flush();
            out.close();
        }
    
        @Test
        public void testFreeMarker3() throws Exception {
            Template template = getTemplate("模板3.ftl");
            
            // 第七步:创建模板需要的数据集。可以是一个map对象也可以是一个pojo,把模板需要的数据都放入数据集。
            Map<String, Object> root = new HashMap<>();
            Student stu = new Student(1001, "张三", "地球");
            Student stu2 = new Student(1002, "李四", "火星");
            Student stu3 = new Student(1003, "小明", "不详");
            Student stu4 = new Student(1004, "小明", "不详");
            Student stu5 = new Student(1005, "小明", "不详");
            Student stu6 = new Student(1006, "小明", "不详");
            List<Student> studentList = new ArrayList<>();
            studentList.add(stu);
            studentList.add(stu2);
            studentList.add(stu3);
            studentList.add(stu4);
            studentList.add(stu5);
            studentList.add(stu6);
    
            root.put("title", "hello freemarker");
            root.put("student", stu);
            root.put("studentList", studentList);
    
            // 第八步:创建一个Writer对象,指定生成的文件保存的路径及文件名。
            Writer out = new FileWriter(new File("D:\temp\html\静态页面3.html"));
            // 第九步:调用模板对象的process方法生成静态文件。需要两个参数数据集和writer对象。
            template.process(root, out);
            // 第十步:关闭writer对象。
            out.flush();
            out.close();
        }
        
        private Template getTemplate(String templateFileName) throws Exception {
            // 第一步:把freemarker的jar包添加到工程中
            // 第二步:freemarker的运行不依赖web容器,可以在java工程中运行。创建一个测试方法进行测试。
            // 第三步:创建一个Configuration对象
            Configuration configuration = new Configuration(Configuration.getVersion());
            // 第四步:告诉config对象模板文件存放的路径。
            configuration.setDirectoryForTemplateLoading(
                    new File("D:\oy_workspace\eclipse_javaee_win64_workspace\test_freemarker\ftl"));
            // 第五步:设置config的默认字符集。一般是utf-8
            configuration.setDefaultEncoding("utf-8");
            // 第六步:从config对象中获得模板对象。需要制定一个模板文件的名字。
            // eclipse不支持*.ftl文件,可以将第三方提供的插件freemarker-ide-0.9.14复制到eclipse的dropins目录下
            Template template = configuration.getTemplate(templateFileName);
            
            return template;
        }
    }

      

      first.ftl

    ${hello}

      second.ftl

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>${title}</title>
        </head>
        <body>
            <label>学号:</label>${student.id}<br>
            <label>姓名:</label>${student.name}<br>
            <label>住址:</label>${student.address}<br>
        </body>
    </html>

      模板3.ftl

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>${title}</title>
        </head>
        <body>
            <label>学号:</label>${student.id}<br>
            <label>姓名:</label>${student.name}<br>
            <label>住址:</label>${student.address}<br>
        </body>
        列表展示:
        <table border="1" bordercolor="blue" cellspacing="0" width="50%">
            <th>
                <td align="center">学号</td>
                <td align="center">姓名</td>
                <td align="center">住址</td>
            </th>
            <#list studentList as stu>
            <#if stu_index%2==0>
            <tr style="color:red">
            <#else>
            <tr>    
            </#if>
                <td align="center">${stu_index}</td>
                <td align="center">${stu.id}</td>
                <td align="center">${stu.name}</td>
                <td align="center">${stu.address}</td>
            </tr>
            </#list>
        </table>
    </html>
  • 相关阅读:
    HTTP状态码汇总
    树遍历以及图遍历的方法
    HashMap之扩容机制
    MySQL常见的七种锁
    双亲委派机制及作用
    Java进程故障排查思路及步骤
    八大数据结构
    常见的十种排序算法
    使用TortoiseGit操作分支的创建与合并
    Storage Port Drivers
  • 原文地址:https://www.cnblogs.com/xy-ouyang/p/11924056.html
Copyright © 2011-2022 走看看