zoukankan      html  css  js  c++  java
  • FreeMarker之根据模型生成HTML代码

    FreeMarker之根据模型生成HTML代码与FreeMarker根据模型生成Java代码,本质上是一样的,关于生成Java代码可以参考我的这篇文章:FreeMarker之根据模板生成Java代码

    一、导入依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>cn.test.freemarker</groupId>
      <artifactId>testFreemarker</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.freemarker</groupId>
          <artifactId>freemarker</artifactId>
          <version>2.3.23</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>testFreemarker</finalName>
      </build>
    
    </project>

    二、编写模板

    在src/main/java下新建templates包,并在该包下新建user.ftl,内容如下所示:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>用户信息</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet"
        href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" />
    </head>
    <body style="font-family:'Courier New'">
        <h3 class="text-center">这是用户${username}的信息页!</h3>
        <div class="col-md-6 column">
            <table class="table table-bordered">
                <tr>
                    <th>用户名</th>
                    <th>密码</th>
                    <th>年龄</th>
                    <th>地址</th>
                </tr>
                <tr>
                    <td>${username}</td>
                    <td>${password}</td>
                    <td>${age}</td>
                    <td>${address}</td>
                </tr>
            </table>
        </div>
    </body>
    </html>

    三、编写Java代码

    在src/main/java新增testFreeMarker包,并在该包下创建该两个Java文件

    User.java

    package testFreemarker;
    
    public class User {
    
        private String username;
    
        private String password;
    
        private Integer age;
    
        private String address;
    
        
        public User() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public User(String username, String password, Integer age, String address) {
            super();
            this.username = username;
            this.password = password;
            this.age = age;
            this.address = address;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
      
    }

    CreateHtmlByFreemarker.java

    package testFreemarker;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.HashMap;
    import java.util.Map;
    
    import freemarker.core.ParseException;
    import freemarker.template.Configuration;
    import freemarker.template.MalformedTemplateNameException;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    import freemarker.template.TemplateNotFoundException;
    
    public class CreateHtmlByFreemarker {
            private static final String TEMPLATE_PATH = "src/main/java/templates";
            private static final String CLASS_PATH = "src/main/webapp/product";
            
            public static void main(String[] args) {
                // step1 创建freeMarker配置实例
                Configuration configuration = new Configuration();
                Writer out = null;
                try {
                    // step2 获取模版路径
                    configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
                    // step3 创建数据模型
                    Map<String, Object> dataMap = new HashMap<String, Object>();
                    dataMap.put("username", "zhangsan");
                    dataMap.put("password", "123456");
                    dataMap.put("age", "18");
                    dataMap.put("address", "test");
                    // step4 加载模版文件
                    Template template = configuration.getTemplate("user.ftl");
                    // step5 生成数据
                    File docFile = new File(CLASS_PATH + "\" + "user.html");
                    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
                    // step6 输出文件
                    template.process(dataMap, out);
                    System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^user.ftl 文件创建成功 !");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (null != out) {
                            out.flush();
                        }
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            }
    
    }

    四、运行CreateHtmlByFreemarker.java

    一般运行不报错并输出user.ftl创建成功的输出语句就表示成功,这时只需刷新下,就可以在对应的目录下看到通过模板生成的html文件。

  • 相关阅读:
    第二阶段:冲刺2(个人界面的优化---头像上传)
    第二阶段:冲刺1(任务的分配)
    学习进度第12周
    读书笔记《人月神话》1
    学习进度第11周
    读书笔记《编程珠矶》3
    学习进度第10周
    第一阶段:冲刺10(完结)
    第十五周学习进度总结
    第十四周学习进度总结
  • 原文地址:https://www.cnblogs.com/youcong/p/9520730.html
Copyright © 2011-2022 走看看