1. 程式人生 > >web開發-Thymeleaf、FreeMarker模板引擎

web開發-Thymeleaf、FreeMarker模板引擎

Spring Boot 推薦使用Thymeleaf、FreeMarker、Velocity、Groovy、Mustache等模板引擎。不建議使用JSP。

Spring Boot 對以上幾種引擎提供了良好的預設配置,預設 src/main/resources/templates 目錄為以上模板引擎的配置路徑。

一、Spring Boot 中使用Thymeleaf模板引擎

簡介:Thymeleaf 是類似於Velocity、FreeMarker 的模板引擎,可用於Web與非Web環境中的應用開發,並且可以完全替代JSP 。

1、pom.xml 新增依賴

<!-- thymeleaf 模板引擎-->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

2、編寫controller

/**
 * @author sam
 * @since 2017/7/16
 */
@Controller
public class HomeController {

    @RequestMapping("/home")
    public
String home(ModelMap modelMap) { modelMap.put("name", "Magical Sam"); List<String> list = new ArrayList<>(); list.add("sam a"); list.add("sam b"); list.add("sam c"); list.add("sam d"); modelMap.put("list", list); return "home"
; } }

3、編寫html程式碼,其中th:text="${name}" 為thymeleaf的語法,具體可參考:Thymeleaf 官方文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Home</title>
</head>
<body>
    <span th:text="${name}"></span>
    <ul>
        <li th:each="item : ${list}" th:text="${item}"></li>
    </ul>
</body>
</html>

啟動應用,訪問:http://localhost:8080/home ,可以得到相應結果。

如需修改 thymeleaf 的預設配置,可以在application.properties中新增:

# ================================================
#                   Thymeleaf配置
# ================================================
# 是否啟用thymeleaf模板解析
spring.thymeleaf.enabled=true
# 是否開啟模板快取(建議:開發環境下設定為false,生產環境設定為true)
spring.thymeleaf.cache=false 
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true 
# 模板的媒體型別設定,預設為text/html
spring.thymeleaf.content-type=text/html
# 模板的編碼設定,預設UTF-8
spring.thymeleaf.encoding=UTF-8
# 設定可以被解析的檢視,以逗號,分隔
#spring.thymeleaf.view-names=
# 排除不需要被解析檢視,以逗號,分隔
#spring.thymeleaf.excluded-view-names=
# 模板模式設定,預設為HTML5
#spring.thymeleaf.mode=HTML5 
# 字首設定,SpringBoot預設模板放置在classpath:/template/目錄下
spring.thymeleaf.prefix=classpath:/templates/ 
# 字尾設定,預設為.html
spring.thymeleaf.suffix=.html
# 模板在模板鏈中被解析的順序
#spring.thymeleaf.template-resolver-order=

二、Spring Boot 中使用FreeMarker模板引擎

1、pom.xml 新增依賴

<!-- freemarker 模板引擎 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、編寫controller

同上。

3、templates 下新建 home.ftl檔案編寫html程式碼,freemarker語法 可參考:FreeMarker 官方文件

home.ftl
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <span>${name}</span>
    <ul>
    <#list list as item >
        <li>${item}</li>
    </#list>
    </ul>
</body>
</html>

啟動應用,訪問:http://localhost:8080/home ,可以得到相應結果。

如需修改 freemarker 的預設配置,可以在application.properties中新增:

# ================================================
#                   FreeMarker配置
# ================================================
# 是否開啟模板快取
spring.freemarker.cache=true
# 編碼格式
spring.freemarker.charset=UTF-8
# 模板的媒體型別設定
spring.freemarker.content-type=text/html
# 字首設定 預設為 ""
spring.freemarker.prefix=
# 字尾設定 預設為 .ftl
spring.freemarker.suffix=.ftl
#spring.freemarker.allow-request-override=false
#spring.freemarker.check-template-location=true
#spring.freemarker.expose-request-attributes=false
#spring.freemarker.expose-session-attributes=false
#spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.request-context-attribute=
#spring.freemarker.template-loader-path=classpath:/templates/
#spring.freemarker.view-names=

附: application.properties 全部配置項,點選檢視Spring Boot 所有配置說明