Spring Boot 系列(五)web開發-Thymeleaf、FreeMarker模板引擎


前面幾篇介紹了返回json數據提供良好的RESTful api,下面我們介紹如何把處理完的數據渲染到頁面上。

Spring Boot 使用模板引擎

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 所有配置說明

版權聲明:本文為博主原創文章,轉載請注明出處。


免責聲明!

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



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