使用FreeMarker生成静态HTML

1、FreeMarker需要添加的Maven依赖:

1 <dependency>
2     <groupId>org.freemarker</groupId>
3     <artifactId>freemarker</artifactId>
4     <version>2.3.23</version>
5 </dependency>

2、使用模板生成HTML代码

2.1 数据模型

 1 public class User {
 2 
 3     private String username;
 4 
 5     private String password;
 6 
 7     private Integer age;
 8 
 9     private String address;
10 
11     //省略setter和getter方法  
12 }

 

2.2 FreeMarker模板

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>用户信息</title>
 6 <!-- 新 Bootstrap 核心 CSS 文件 -->
 7 <link rel="stylesheet"
 8     href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" />
 9 </head>
10 <body style="font-family:'Courier New'">
11     <h3 class="text-center">这是用户${username}的信息页!</h3>
12     <div class="col-md-6 column">
13         <table class="table table-bordered">
14             <tr>
15                 <th>用户名</th>
16                 <th>密码</th>
17                 <th>年龄</th>
18                 <th>地址</th>
19             </tr>
20             <tr>
21                 <td>${username}</td>
22                 <td>${password}</td>
23                 <td>${age}</td>
24                 <td>${address}</td>
25             </tr>
26         </table>
27     </div>
28 </body>
29 </html>

2.3 使用FreeMarker生成HTML代码

 1     /**
 2      * 使用模板生成HTML代码
 3      */
 4     public static void createHtmlFromModel() {
 5         FileWriter out = null;
 6         try {
 7             // 通过FreeMarker的Confuguration读取相应的模板文件
 8             Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
 9             // 设置模板路径
10             configuration.setClassForTemplateLoading(CreateHtmlByFreemarker.class, "/static/ftl");
11             // 设置默认字体
12             configuration.setDefaultEncoding("utf-8");
13 
14             // 获取模板
15             Template template = configuration.getTemplate("user.ftl");
16             //设置模型
17             User user = new User("tom", "hahahah", 28, "上海市");
18             
19             //设置输出文件
20             File file = new File("e:/html/result.html");
21             if(!file.exists()) {
22                 file.createNewFile();
23             }
24             //设置输出流
25             out = new FileWriter(file);
26             //模板输出静态文件
27             template.process(user, out);
28         } catch (TemplateNotFoundException e) {
29             e.printStackTrace();
30         } catch (MalformedTemplateNameException e) {
31             e.printStackTrace();
32         } catch (ParseException e) {
33             e.printStackTrace();
34         } catch (IOException e) {
35             e.printStackTrace();
36         } catch (TemplateException e) {
37             e.printStackTrace();
38         } finally {
39             if(null != out) {
40                 try {
41                     out.close();
42                 } catch (IOException e) {
43                     e.printStackTrace();
44                 }
45             }
46         }
47     }

3、使用String作为FreeMarker模板,生成HTML代码

3.1 数据模型使用2.1模型

3.2 模板使用2.2模板

3.3 使用FreeMarker生成HTML代码

 1 /**
 2      * 把模板读入到String中,然后根据String构造FreeMarker模板
 3      */
 4     public static void createHtmlFromString() {
 5         BufferedInputStream in = null;
 6         FileWriter out = null;
 7         try {
 8             //模板文件
 9             File file = new File("D:/EclipseLearnSpace/ResearchSpace/Html2Pdf/src/main/resources/static/html/user.html");
10             //构造输入流
11             in = new BufferedInputStream(new FileInputStream(file));
12             int len;
13             byte[] bytes = new byte[1024];
14             //模板内容
15             StringBuilder content = new StringBuilder();
16             while((len = in.read(bytes)) != -1) {
17                 content.append(new String(bytes, 0, len, "utf-8"));
18             }
19             
20             //构造Configuration
21             Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
22             //构造StringTemplateLoader
23             StringTemplateLoader loader = new StringTemplateLoader();
24             //添加String模板
25             loader.putTemplate("test", content.toString());
26             //把StringTemplateLoader添加到Configuration中
27             configuration.setTemplateLoader(loader);
28             
29             //构造Model
30             User user = new User("tom", "kkkkk", 29, "北京山东");
31             //获取模板
32             Template template = configuration.getTemplate("test");
33             //构造输出路
34             out = new FileWriter("e:/html/result.html");
35             //生成HTML
36             template.process(user, out);
37         } catch (FileNotFoundException e) {
38             e.printStackTrace();
39         } catch (UnsupportedEncodingException e) {
40             e.printStackTrace();
41         } catch (IOException e) {
42             e.printStackTrace();
43         } catch (TemplateException e) {
44             e.printStackTrace();
45         } finally {
46             if(null != in) {
47                 try {
48                     in.close();
49                 } catch (IOException e) {
50                     e.printStackTrace();
51                 }
52             }
53             if(null != out) {
54                 try {
55                     out.close();
56                 } catch (IOException e) {
57                     e.printStackTrace();
58                 }
59             }
60         }
61         
62     }

4、使用String模板,模板中使用List

4.1 数据模型

 1 public class Classes {
 2 
 3     private String classId; // 班级ID
 4 
 5     private String className; // 班级名称
 6 
 7     private List<User> users;
 8 
 9     public String getClassId() {
10         return classId;
11     }
12 
13     //省略settter和getter方法
14 
15 }

4.2 FreeMarker模板

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>班级信息</title>
 6 <!-- 新 Bootstrap 核心 CSS 文件 -->
 7 <link rel="stylesheet"
 8     href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" />
 9 <style type="text/css">
10     .table-align{
11         margin-left: 25%;
12     }
13 </style>
14 </head>
15 <body style="font-family:'Courier New'">
16 
17     <h3 class="text-center">下面是班级ID【${classId}】,班级名称【${className}】的人员信息</h3>
18     <div class="col-md-6 column table-align">
19         <table class="table">
20             <tr>
21                 <th>姓名</th>
22                 <th>密码</th>
23                 <th>年龄</th>
24                 <th>地址</th>
25             </tr>
26             <!-- FreeMarker使用List循环 -->
27             <#list users as user>
28                 <tr>
29                     <td>${user.username}</td>
30                     <td>${user.password}</td>
31                     <td>${user.age}</td>
32                     <td>${user.address}</td>
33                 </tr>
34             </#list>
35         </table>
36     </div>
37 </body>
38 </html>

4.3 使用FreeMarker生成HTML代码

 1 /**
 2      * 根据String模板生成HTML,模板中存在List循环
 3      */
 4     public static void createHtmlFromStringList() {
 5         BufferedInputStream in = null;
 6         FileWriter out = null;
 7         try {
 8             //模板文件
 9             File file = new File("D:/EclipseLearnSpace/ResearchSpace/Html2Pdf/src/main/resources/static/html/class.html");
10             //构造输入流
11             in = new BufferedInputStream(new FileInputStream(file));
12             int len;
13             byte[] bytes = new byte[1024];
14             //模板内容
15             StringBuilder content = new StringBuilder();
16             while((len = in.read(bytes)) != -1) {
17                 content.append(new String(bytes, 0, len, "utf-8"));
18             }
19             
20             //构造Configuration
21             Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
22             //构造StringTemplateLoader
23             StringTemplateLoader loader = new StringTemplateLoader();
24             //添加String模板
25             loader.putTemplate("test", content.toString());
26             //把StringTemplateLoader添加到Configuration中
27             configuration.setTemplateLoader(loader);
28             
29             //构造Model
30             Classes classes = new Classes();
31             classes.setClassId("23");
32             classes.setClassName("实验一班");
33             List<User> users = new ArrayList<User>();
34             User user = new User("tom", "kkkkk", 29, "北京");
35             users.add(user);
36             User user2 = new User("Jim", "hhhh", 22, "上海");
37             users.add(user2);
38             User user3 = new User("Jerry", "aaaa", 25, "南京");
39             users.add(user3);
40             classes.setUsers(users);
41             //获取模板
42             Template template = configuration.getTemplate("test");
43             //构造输出路
44             out = new FileWriter("e:/html/result.html");
45             //生成HTML
46             template.process(classes, out);
47         } catch (FileNotFoundException e) {
48             e.printStackTrace();
49         } catch (UnsupportedEncodingException e) {
50             e.printStackTrace();
51         } catch (IOException e) {
52             e.printStackTrace();
53         } catch (TemplateException e) {
54             e.printStackTrace();
55         } finally {
56             if(null != in) {
57                 try {
58                     in.close();
59                 } catch (IOException e) {
60                     e.printStackTrace();
61                 }
62             }
63             if(null != out) {
64                 try {
65                     out.close();
66                 } catch (IOException e) {
67                     e.printStackTrace();
68                 }
69             }
70         }
71         
72     }

 

posted @ 2016-10-31 15:00  自在流云  阅读(21792)  评论(0编辑  收藏  举报