FreeMarker 跟 Thymeleaf 一樣,是一種模板引擎,他可以無縫兼容 FreeMarker 在 Spring Boot 開發者中仍然有着很高的地位。
本章重點內容
- 編寫一個最簡單的 Freemark 模板示例
- 簡單說明 FreeMarker
1 FreeMarker 簡介
相對於 Jsp ,FreeMarker 具有太多的優勢。FreeMarker 適合 Mvc 場景。
FreeMarker 最大的特點就是具有可編程能力,可以對任何后台輸出的數據做編程能力,這就像在 Java 中加入了 PHP 功能,這非常有趣。
FreeMarker 支持各類語法包括 字符輸出、條件判斷 if/else、循環遍歷、
1.1 變量
${...}
1.2 條件語句
<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>
1.3 循環語句
假設 users 包含['Joe', 'Kate', 'Fred'] 序列:
<#list users as user>
<p>${user}
</#list>
輸出:
<p>Joe
<p>Kate
<p>Fred
1.4 include 包含語句
將版權信息單獨存放在頁面文件 copyright_footer.html 中:
<hr>
<i>
Copyright (c) 2000 <a href="http://www.baidu.com">Baidu Inc</a>,
<br>
All Rights Reserved.
</i>
當需要用到這個文件時,可以使用 include 指令來插入:
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<p>Blah blah...
<#include "/copyright_footer.html">
</body>
</html>
2 Spring Boot 中編寫一個 FreeMarker 示例
本示例文件結構,新增了連個用於示例文件的文件 IndexController.java 與 index.ftl
+ java/fishpro/freemarker/controller
-- IndexController.java
+ resources/templates
--index.ftl
2.1 新建一個 Spring Boot 的 Maven 項目
2.1 新建 Spring Boot 項目
- File > New > Project,如下圖選擇
Spring Initializr
然后點擊 【Next】下一步 - 填寫
GroupId
(包名)、Artifact
(項目名) 即可。點擊 下一步
groupId=com.fishpro
artifactId=freemarker - 選擇依賴
Spring Web Starter
前面打鈎,在模板列中勾選apache freemarker
。 - 項目名設置為
spring-boot-study-freemarker
.
2.2 編輯 Pom.xml 引入依賴
注意如果在新建下面的時候沒有引入 FreeMarker 那么就在這里復制粘貼上去。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fishpro</groupId>
<artifactId>freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>freemarker</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3 配置文件
注意 Spring Boot 中默認不需要做任何配置,示例程序把默認的端口 8080 改為了 8086,並把 application.properties 改為了 application.yml
server:
port: 8086
2.4 編寫示例代碼
本示例文件結構,新增了連個用於示例文件的文件 IndexController.java 與 index.ftl
IndexController.java
/**
* 在是一個普通的 Controller 類
* */
@Controller
public class IndexController {
/**
* 路由 /index
* 返回 index 這里默認配置自動映射到 templages/index
* */
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("welcome","hello fishpro");
return "index";
}
}
index.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is welcome ${welcome}
</body>
</html>
2.5 運行示例
在瀏覽器中輸入 http://localhost:8086/index 顯示為,hello fishpro 就是后台輸出的 model 對象
this is welcome hello fishpro
2.6 FreeMarker 實際應用場景
FreeMarker 比傳統的 JSP 多出一個模板的作用,就是說,我們可以做多個不同樣式的模板,動態的切換。這就是 FreeMarker 應用場景。