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,運行效果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM