1. 程式人生 > >在Spring Boot中使用Thymeleaf模板引擎

在Spring Boot中使用Thymeleaf模板引擎

官網關於thymeleaf的介紹已經很詳細了!總結起來就一句話:thymeleaf使得同一個頁面模板對前端和後端都很友好(雙贏),對於前端來說它就是一個靜態頁面,對於後端來說它便是動態頁面!

thymeleaf是怎麼做到這一點的呢? 其實thmeleaf提供了一堆侵入性很小的標籤給後端人員使用,這些標籤的插入位置全部都作為html標籤的屬性存在。對於瀏覽器來說這些非HTML標準屬性在渲染的時候會被瀏覽器自動忽略,因此對於前端人員來說它就是一個靜態頁面;而後端通過thymeleaf引擎渲染的時候這些標籤就會被識別,因此對於後端人員來說它又是動態頁面!

下面通過一個簡單的例子介紹如何在Spring Boot專案中使用thymeleaf。

thymeleaf的配置

1、在pom.xml檔案中新增依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId
>
spring-boot-starter-thymeleaf</artifactId> </dependency>

其中,spring-boot-starter-web用於支援web開發,spring-boot-starter-thymeleaf用於支援thymeleaf模板引擎。

2、配置屬性

其實完全可以直接使用,不用配置。但是Spring Boot官方文件建議在開發時將快取關閉,那就在application.properties檔案中加入下面這行就行了:

spring.thymeleaf.cache=false

一個簡單的例子

1、編寫User類

package com.tao.springboot.domain;

public class User {

    private String name;
    private int age;

    public User(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

2、編寫Controller類

package com.tao.springboot.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tao.springboot.domain.User;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String userList(Model model) throws Exception {
         model.addAttribute("title", "使用者列表");
         model.addAttribute("hello","Hello, Spring Boot!");
         List<User> userList = new ArrayList<>();
         userList.add(new User("小明", 25));
         userList.add(new User("小黃", 23));
         model.addAttribute("userList", userList);

         return "/user/list";
    }
}

3、建立thymeleaf模板檔案

thymeleaf在Spring Boot中預設的模板配置路徑為:src/main/resources/templates,因此我們在resources資料夾下面建立一個templates資料夾,然後建立/user/list.html檔案:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
<meta charset="UTF-8" />
<title th:text="${title}">標題</title>
</head>
<body>
    <h1 th:text="${hello}">hello</h1>
    <table>
        <tr>
            <th>姓名</th>
            <th>年齡</th>
        </tr>
        <tr th:each="user : ${userList}">
            <td th:text="${user.name}">小明</td>
            <td th:text="${user.age}">20</td>
        </tr>
    </table>

    <select>
        <option th:each="user : ${userList}" th:value="${user.name}" th:text="${user.name}">我是預設值</option>
    </select>

</body>
</html>

4、啟動程式

相關推薦

Spring Boot 整合 Thymeleaf 模板引擎

原文地址:Spring Boot 整合 Thymeleaf 模板引擎 Thymeleaf 是適用於 Web 和獨立環境的現代伺服器端 Java 模板引擎。 Thymeleaf 的主要目標是為開發帶來優雅的模板 - Thymeleaf 模板對於瀏覽器來說這些非HTML標準屬性在渲染

Spring Boot使用Thymeleaf模板引擎渲染web檢視

Spring Boot開發Web應用 原創   2018-04-03  宗野   Spring Boot Spring Boot快速入門中我們完成了一個簡單的RESTfu

Spring Boot整合Thymeleaf模板引擎

一、Thymeleaf 模板介紹 Spring Boot 推薦使用Thymeleaf 來代替傳統開發中的JSP,那麼什麼是Thymeleaf 模板引擎呢?下面就來簡單的介紹一下。 1.1什麼是Thymeleaf Thymeleaf 是一款用於渲

Spring Boot2(五):使用Spring Boot結合Thymeleaf模板引擎使用總結

一、Thymeleaf概述 一般來說,常用的模板引擎有JSP、Velocity、Freemarker、Thymeleaf 。 SpringBoot推薦的 Thymeleaf – 語法更簡單,功能更強大; Thymeleaf是一種Java XML/XHTML/HTML5模板引擎,可以在Web和非Web環境中使用

spring boot集成模板引擎Thymeleaf遇到的坑

固定 con sources index pac 調用 return div template 首先,所有html文件都要放在固定路徑下才能被正確讀取到,/main/java/resources/templates這個路徑下,而且html所有的標簽必須閉合,否則啟動報錯 今

Spring Boot整合之模板引擎Thymeleaf、Freemarker、jsp)

1. Thymeleaf模板 1.1 在pom.xml中新增Thymeleaf依賴 <!--使用thymeleaf標籤--> <dependency> <groupId>org.springframework.boot</groupId>

Spring Boot 整合之模板引擎(jsp、Freemarker 、Thymeleaf

整合JSP模板   新增依賴 建立 maven 工程,在 pom.xml 檔案中新增如下依賴: <dependency> <groupId>javax.servlet</groupId> <artifactId>

Spring Boot使用thymeleaf模板時報異常:template might not exist or might not be accessible by any of the configured Template Resolvers

logs pla 開頭 spring 方法 temp ring mode acc 錯誤如下: template might not exist or might not be accessible by any of the configured Template R

Spring Boot使用Freemarker模板引擎渲染web檢視

Spring Boot使用Freemarker模板引擎渲染檢視,開發Web應用 原創   2018-04-03  宗野   Spring Boot Spring Boot

spring boot使用thymeleaf模板的方法詳解

thymeleaf 是新一代的模板引擎,在spring4.0中推薦使用thymeleaf來做前端模版引擎。 前言 Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。相較與其他的模板引擎,它有如下三個極吸引人的特點:  

spring-boot--使用thymeleaf模板

轉:http://jisonami.iteye.com/blog/2301387,http://412887952-qq-com.iteye.com/blog/2292402 整體步驟: (1)            在pom.xml中引入thymeleaf; (2)   

Spring Boot整理——Thymeleaf模板

一、基本介紹         Thymeleaf是一個Java庫。它是一個XML / XHTML / HTML5模板引擎,能夠應用於轉換模板檔案,以顯示您的應用程式產生的資料和文字。它尤其適合於基於XHTML / HTML5的web服務應用程式,同時它可以處理任何XML檔案

如何去掉Spring Bootthymeleaf模版引擎強制校驗

筆者在使用Spring Boot 的thymeleaf模版引擎做html頁面時。遇到一個很頭疼的問題,那就是thymeleaf模版引擎強制校驗,由於是網上找的bootstrap模版,用的h5,頁面很多標籤都沒有閉合標籤/,就會報錯跳轉到錯誤頁面。 html頁面如下:

Spring Boot使用Thymeleaf模板引擎

官網關於thymeleaf的介紹已經很詳細了!總結起來就一句話:thymeleaf使得同一個頁面模板對前端和後端都很友好(雙贏),對於前端來說它就是一個靜態頁面,對於後端來說它便是動態頁面! thymeleaf是怎麼做到這一點的呢? 其實thmeleaf提供了

Thymeleaf 模板spring boot 的引用和應用

end text www. bean template har ica ngs sta Thymeleaf是一個java類庫,他是一個xml/xhtml/html5的模板引擎和Struts框架的freemarker模板類似,可以作為mvc的web應用的view層。 Thy

spring boot Thymeleaf模板引擎 最簡單輸出例子

test leaf att map 控制器 輸出 span blog hello spring boot Thymeleaf模板引擎 最簡單輸出例子 控制器代碼如下: @GetMapping(value = "/test")public String test(Mo

Spring Boot 2.0 整合Thymeleaf 模板引擎

reporting 配置信息 name www. title 建模 type 引擎 suffix 本節將和大家一起實戰Spring Boot 2.0 和thymeleaf 模板引擎 1. 創建項目 2. 使用Spring Initlizr 快速創建Spring Boot

spring boot: thymeleaf模板引擎使用

sage homepage 順序 object tomcat fig component art format spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依賴 <!-- 添加thymeleaf的依賴 --

Spring MVC使用Thymeleaf模板引擎

由Ricky發表在天碼營 新一代Java模板引擎Thymeleaf一定讓你驚歎於Thymeleaf的強大,但是真正在Web應用結合Web特性使用模板引擎,還需要進行一定的配置和學習。 Thymeleaf於Spring整合 Thymeleaf除了基本的模板引擎,還提供了一套Spring整合技術使得在Sprin

Spring Boot學習筆記-Thymeleaf模板引擎的配置

寫在前面   在開發過程中,使用模板引擎是很有必要的。我之前學習SSM框架,一直是使用Freemarker作為模板引擎,現在學習了Spring Boot之後,知道Spring Boot推薦使用的模板引擎是Thymeleaf,那麼,對於我這種有點輕微強迫症的人來