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

前言

因爲在開發My Blog項目時使用了大量的技術整合,針對於部分框架的使用和整合的流程沒有作詳細的介紹和記錄,致使有些朋友用起來有些吃力,所以打算在接下來的時間裏作一些基礎整合的介紹,固然,可能也不會特別的基礎,可是源碼會開放給你們,方便你們學習,這次的源碼地址爲springboot-thymeleaf,多謝你們支持。html

簡介

Thymeleaf
Thymeleaf是一個跟Velocity、FreeMarker相似的模板引擎,它能夠徹底替代JSP,相較與其餘的模板引擎,它有以下三個極吸引人的特色:java

  • 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。git

<!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 :github

/**
 * 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-thymeleafspring

結語

首發於個人我的博客apache

若是有問題或者有一些好的創意,歡迎給我留言,也感謝向我指出項目中存在問題的朋友。瀏覽器

代碼和此次的問題都是My Blog項目中的,若是你想繼續瞭解該項目能夠查看整個系列文章Java開源博客My-Blog(SpringBoot+Docker)系列文章,也能夠到個人GitHub倉庫或者開源中國代碼倉庫中查看源碼及詳細的部署過程和使用文檔。springboot

相關文章
相關標籤/搜索