第一個(gè) Thymeleaf 模板頁

學(xué)習(xí)完整課程請移步 互聯(lián)網(wǎng) Java 全棧工程師

本節(jié)視頻

引入依賴

主要增加 spring-boot-starter-thymeleafnekohtml 這兩個(gè)依賴

  • spring-boot-starter-thymeleaf:Thymeleaf 自動(dòng)配置
  • nekohtml:允許使用非嚴(yán)格的 HTML 語法

完整的 pom.xml 如下:

<?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>

    <groupId>com.funtl</groupId>
    <artifactId>hello-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hello-spring-boot</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.funtl.hello.spring.boot.HelloSpringBootApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml 中配置 Thymeleaf

spring:
  thymeleaf:
    cache: false # 開發(fā)時(shí)關(guān)閉緩存,不然沒法看到實(shí)時(shí)頁面
    mode: HTML # 用非嚴(yán)格的 HTML
    encoding: UTF-8
    servlet:
      content-type: text/html

創(chuàng)建測試用 JavaBean

創(chuàng)建一個(gè)測試效果的 JavaBean,簡單封裝一下即可

package com.funtl.hello.spring.boot.entity;

import java.io.Serializable;

public class PersonBean implements Serializable {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

創(chuàng)建測試用 Controller

創(chuàng)建一個(gè) Controller,造一些測試數(shù)據(jù)并設(shè)置跳轉(zhuǎn)

package com.funtl.hello.spring.boot.controller;

import com.funtl.hello.spring.boot.entity.PersonBean;
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 java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "thymeleaf")
public class IndexController {

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(Model model) {
        PersonBean person = new PersonBean();
        person.setName("張三");
        person.setAge(22);

        List<PersonBean> people = new ArrayList<>();
        PersonBean p1 = new PersonBean();
        p1.setName("李四");
        p1.setAge(23);
        people.add(p1);

        PersonBean p2 = new PersonBean();
        p2.setName("王五");
        p2.setAge(24);
        people.add(p2);

        PersonBean p3 = new PersonBean();
        p3.setName("趙六");
        p3.setAge(25);
        people.add(p3);

        model.addAttribute("person", person);
        model.addAttribute("people", people);

        return "index";
    }
}

創(chuàng)建測試頁面

templates 目錄下創(chuàng)建 index.html 文件握联,代碼如下:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Hello Thymeleaf</title>
</head>
<body>
    <div>
        <span>訪問 Model:</span><span th:text="${person.name}"></span>
    </div>
    <div>
        <span>訪問列表</span>
        <table>
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>年齡</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="human : ${people}">
                    <td th:text="${human.name}"></td>
                    <td th:text="${human.age}"></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

修改 html 標(biāo)簽用于引入 thymeleaf 引擎快集,這樣才可以在其他標(biāo)簽里使用 th:* 語法,聲明如下:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

測試訪問

啟動(dòng)成功后栅干,訪問:http://localhost:9090/thymeleaf/index 即可看到效果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市捐祠,隨后出現(xiàn)的幾起案子碱鳞,更是在濱河造成了極大的恐慌,老刑警劉巖踱蛀,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件窿给,死亡現(xiàn)場離奇詭異,居然都是意外死亡率拒,警方通過查閱死者的電腦和手機(jī)崩泡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來猬膨,“玉大人角撞,你說我怎么就攤上這事。” “怎么了谒所?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵热康,是天一觀的道長。 經(jīng)常有香客問我劣领,道長姐军,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任尖淘,我火速辦了婚禮奕锌,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘村生。我一直安慰自己惊暴,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布趁桃。 她就那樣靜靜地躺著缴守,像睡著了一般。 火紅的嫁衣襯著肌膚如雪镇辉。 梳的紋絲不亂的頭發(fā)上屡穗,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機(jī)與錄音忽肛,去河邊找鬼村砂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛屹逛,可吹牛的內(nèi)容都是我干的础废。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼罕模,長吁一口氣:“原來是場噩夢啊……” “哼评腺!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起淑掌,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蒿讥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后抛腕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芋绸,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年担敌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了摔敛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,992評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡全封,死狀恐怖马昙,靈堂內(nèi)的尸體忽然破棺而出桃犬,到底是詐尸還是另有隱情,我是刑警寧澤行楞,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布疫萤,位于F島的核電站,受9級特大地震影響敢伸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜恒削,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一池颈、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧钓丰,春花似錦躯砰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至梦鉴,卻和暖如春李茫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背肥橙。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工魄宏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人存筏。 一個(gè)月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓宠互,卻偏偏與公主長得像,于是被迫代替她去往敵國和親椭坚。 傳聞我的和親對象是個(gè)殘疾皇子予跌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評論 2 355