SpringBoot集成Freemarker模板技术

    FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
  模板编写为FreeMarker Template Language (FTL)。它是简单的,专用的语言。该意味着要准备数据在真实编程语言中来显示,比如数据库查询和业务运算,之后模板显示已经准备好的数据。在模板中,你可以专注于如何展现数据,而在模板之外可以专注于要展示什么数据。
  Freemarker的作用主要是将动态页面转换成微静态html页面,提高搜索引擎的收录。具体框架的介绍和用法可参考http://freemarker.foofun.cn。
  SpringBoot框架提供了对Freemarker框架的集成操作,具体操作如下:
1. 加入额外的pom依赖:

1 <!-- Spring Boot Freemarker 模板 依赖 -->
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-freemarker</artifactId>
5 </dependency>

完整pom.xml:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>com.sqy.package</groupId>
 6     <artifactId>spFreemarker</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8 
 9     <parent>
10         <groupId>org.springframework.boot</groupId>
11         <artifactId>spring-boot-starter-parent</artifactId>
12         <version>2.0.4.RELEASE</version>
13         <relativePath /> <!-- lookup parent from repository -->
14     </parent>
15     <!-- 项目设置:编码格式UTF-8 -->
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19         <java.version>1.8</java.version>
20     </properties>
21     <dependencies>
22         <!--单元测试依赖 -->
23         <dependency>
24             <groupId>junit</groupId>
25             <artifactId>junit</artifactId>
26             <version>3.8.1</version>
27             <scope>test</scope>
28         </dependency>
29         <!-- Spring Boot SpringMVC框架依赖 -->
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-web</artifactId>
33         </dependency>
34         <!-- Spring Boot 测试依赖 -->
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40         <!-- 热部署 -->
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-devtools</artifactId>
44             <optional>true</optional>
45             <!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用 devtools,需要重新引入 -->
46             <scope>true</scope><!-- 热部署 -->
47         </dependency>
48         <!-- Spring Boot Freemarker 模板 依赖 -->
49         <dependency>
50             <groupId>org.springframework.boot</groupId>
51             <artifactId>spring-boot-starter-freemarker</artifactId>
52         </dependency>
53     </dependencies>
54     <build>
55         <plugins>
56             <!-- SpringBoot插件 -->
57             <plugin>
58                 <groupId>org.springframework.boot</groupId>
59                 <artifactId>spring-boot-maven-plugin</artifactId>
60             </plugin>
61         </plugins>
62         <!-- SpringBoot项目打包jar名称 -->
63         <finalName>demo</finalName>
64     </build>
65 </project>

2.在application.properties配置freemarker配置或者使用默认模板位置/src/main/resources/templates/及默认后缀 .ftl 。

 1 ## Freemarker 配置
 2 ## 自定义模板文件配置路径 默认模板路径在resources/templates下,默认后缀.ftl
 3 ##spring.freemarker.template-loader-path=classpath:/web/
 4 ##spring.freemarker.cache=false
 5 ##spring.freemarker.charset=UTF-8
 6 ##spring.freemarker.check-template-location=true
 7 ##spring.freemarker.content-type=text/html
 8 ##spring.freemarker.expose-request-attributes=true
 9 ##spring.freemarker.expose-session-attributes=true
10 ##spring.freemarker.request-context-attribute=request
11 ##spring.freemarker.suffix=.ftl

3. 创建启动类。

 1 package com.sqy.start;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Start {
 8     public static void main(String[] args) {
 9         SpringApplication.run(Start.class, args);
10     }
11 }

4.创建Controller

 1 package com.sqy.start.controller;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.ResponseBody;
11 
12 @Controller
13 public class StartController {
14     @RequestMapping("/freeMarker")
15     public String freeMarker(Map<String, Object> map) {
16         map.put("name", "Joe");
17         map.put("sex", 1);
18         
19         List<Map<String, Object>> friends = new ArrayList<Map<String,Object>>();
20         Map<String, Object> friend = new HashMap<String, Object>();
21         friend.put("name", "Jack");
22         friend.put("age", 22);
23         friends.add(friend);
24         friend = new HashMap<String, Object>();
25         friend.put("name", "Tom");
26         friend.put("age", 21);
27         friends.add(friend);
28         map.put("friends", friends);
29         return "freeMarker";
30     }
31 }

5. 在templates文件夹下创建freemarker.flt文件,内容如下:

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
 3 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
 4 <head>
 5   <title>Hello World!</title>
 6 </head>
 7 <body>
 8   <center>
 9     <p>
10       welcome ${name} to freemarker!
11     </p>
12     <p>性别:
13       <#if sex==0>
14         15       <#elseif sex==1>
16         17       <#else>
18         保密
19       </#if>
20     </p>
21     <h4>我的好友:</h4>
22     <#list friends as item>
23       姓名:${item.name} , 年龄${item.age}
24       <br>
25     </#list>
26   </center>
27 </body>
28 </html>

6.启动项目

7.在浏览器中输入localhost:8080/freemarker,运行效果如下:

猜你喜欢

转载自www.cnblogs.com/sqyss/p/10032854.html