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倉庫或者開源中國代碼倉庫中查看源碼及詳細的部署過程和使用文檔。

發佈了81 篇原創文章 · 獲贊 62 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章