使用FreeMarker生成静态HTML

向右看齐 2022-10-01 04:43 341阅读 0赞

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

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

2、使用模板生成HTML代码

2.1 数据模型

复制代码

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

复制代码

2.2 FreeMarker模板

复制代码

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

复制代码

2.3 使用FreeMarker生成HTML代码

复制代码

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

复制代码

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

3.1 数据模型使用2.1模型

3.2 模板使用2.2模板

3.3 使用FreeMarker生成HTML代码

复制代码

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

复制代码

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

4.1 数据模型

复制代码

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

复制代码

4.2 FreeMarker模板

复制代码

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

复制代码

4.3 使用FreeMarker生成HTML代码

复制代码

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

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读