1. 程式人生 > >Springboot系列:Springboot與Thymeleaf模板引擎整合基礎教程(附原始碼)

Springboot系列:Springboot與Thymeleaf模板引擎整合基礎教程(附原始碼)

前言

由於在開發My Blog專案時使用了大量的技術整合,針對於部分框架的使用和整合的流程沒有做詳細的介紹和記錄,導致有些朋友用起來有些吃力,因此打算在接下來的時間裡做一些基礎整合的介紹,當然,可能也不會特別的基礎,但是原始碼會開放給大家,方便大家學習,此次的原始碼地址為springboot-thymeleaf,多謝大家支援。

簡介

Thymeleaf
Thymeleaf是一個跟Velocity、FreeMarker類似的模板引擎,它可以完全替代JSP,相較與其他的模板引擎,它有如下三個極吸引人的特點:

  • Thymeleaf在有網路和無網路的環境下皆可執行,即它可以讓美工在瀏覽器檢視頁面的靜態效果,也可以讓程式設計師在伺服器檢視帶資料的動態頁面效果。這是由於它支援 html 原型,然後在 html 標籤裡增加額外的屬性來達到模板+資料的展示方式。瀏覽器解釋 html 時會忽略未定義的標籤屬性,所以thymeleaf的模板可以靜態地執行;當有資料返回到頁面時,Thymeleaf 標籤會動態地替換掉靜態內容,使頁面動態顯示。
  • Thymeleaf開箱即用的特性。它提供標準和spring標準兩種方言,可以直接套用模板實現JSTL、OGNL表示式效果,避免每天套模板、改jstl、改標籤的困擾。同時開發人員也可以擴充套件和建立自定義的方言。
  • Thymeleaf提供spring標準方言和一個與SpringMVC完美整合的可選模組,可以快速的實現表單繫結、屬性編輯器、國際化等功能。

整合過程

編輯pom檔案,引入Thymeleaf

<?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>
    <artifactId>com.my.blog</artifactId>
    <name>springboot-thymeleaf</name>
    <description>springboot-thymeleaf</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Thymeleaf配置

# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false

新建模板檔案

在resources資料夾下新增templates目錄,用於存放模板檔案,新增hello.html。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>springboot-thymeleaf demo</title>
</head>
<body>
    <p th:text="'hello, ' + ${name} + '!'" />
</body>
</html>

編輯業務程式碼及啟動類

HelloController :

/**
 * author:13
 * date:2017-09-14
 */
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(HttpServletRequest request, @RequestParam(value = "name", required = false, defaultValue = "springboot-thymeleaf") String name) {
        request.setAttribute("name", name);
        return "hello";
    }
}

WebApplication :

/**
 * author:13
 * date:2017-09-14
 */
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}

驗證

專案啟動後,在瀏覽器端輸入以下urlhttp://localhost:8080/hello
hello-thymeleaf

結語

首發於我的個人部落格。

如果有問題或者有一些好的創意,歡迎給我留言,也感謝向我指出專案中存在問題的朋友。

程式碼和這次的問題都是My Blog專案中的,如果你想繼續瞭解該專案可以檢視整個系列文章Java開源部落格My-Blog(SpringBoot+Docker)系列文章,也可以到我的GitHub倉庫或者開源中國程式碼倉庫中檢視原始碼及詳細的部署過程和使用文件。