溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot中怎么搭建Thymeleaf環(huán)境

發(fā)布時(shí)間:2021-07-08 16:37:12 來(lái)源:億速云 閱讀:153 作者:Leah 欄目:大數(shù)據(jù)

SpringBoot中怎么搭建Thymeleaf環(huán)境,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

1. 依賴

首先我們是需要一個(gè)springboot項(xiàng)目,基本的pom結(jié)構(gòu)大都相似

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

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

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

在這個(gè)項(xiàng)目中,我們主要需要引入兩個(gè)依賴包,一個(gè)web,一個(gè)thymeleaf

<dependencies>
    <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>
</dependencies>

2. 配置參數(shù)

通常我們直接使用默認(rèn)的thymeleaf參數(shù)配置即可,下面給出幾個(gè)常用的配置

spring:
  thymeleaf:
    mode: HTML
    encoding: UTF-8
    servlet:
      content-type: text/html
    cache: false

thymeleaf的參數(shù),主要對(duì)應(yīng)的是org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties

II. 項(xiàng)目搭建演示

1. 項(xiàng)目結(jié)構(gòu)

搭建一個(gè)web項(xiàng)目和我們之前的純后端項(xiàng)目有點(diǎn)不一樣,前端資源放在什么地方,依賴文件怎么處理都是有講究的,下面是一個(gè)常規(guī)的項(xiàng)目結(jié)構(gòu)

SpringBoot中怎么搭建Thymeleaf環(huán)境

如上圖,前端資源文件默認(rèn)放在resources目錄下,下面有兩個(gè)目錄

  • templates:存放模板文件,可以理解為我們編寫(xiě)的html,注意這個(gè)文件名不能有問(wèn)題

  • static: 存放靜態(tài)資源文件,如js,css,image等

2. Rest服務(wù)

我們這里提供了三個(gè)接口,主要是為了演示三種不同的數(shù)據(jù)綁定方式(和Freemaker這篇博文基本一樣)

@Controller
public class IndexController {

    @GetMapping(path = {"", "/", "/index"})
    public ModelAndView index() {
        Map<String, Object> data = new HashMap<>(2);
        data.put("name", "YiHui Thymeleaf");
        data.put("now", LocalDateTime.now().toString());
        return new ModelAndView("index", data);
    }

    /**
     * 一般不建議直接使用jdk的String.split來(lái)分割字符串,內(nèi)部實(shí)現(xiàn)是根據(jù)正則來(lái)處理的,雖然更強(qiáng)大,但在簡(jiǎn)單的場(chǎng)景下,性能開(kāi)銷(xiāo)更大
     */
    private static String[] contents =
            ("綠蟻浮觴香泛泛,黃花共薦芳辰。\n清霜天宇凈無(wú)塵。\n登高宜有賦,拈筆戲成文。\n可奈園林搖落盡,悲秋意與誰(shuí)論。\n眼中相識(shí)幾番新。\n龍山高會(huì)處,落帽定何人。").split("\n");
    private static Random random = new Random();

    @GetMapping(path = "show1")
    public String showOne(Model model) {
        model.addAttribute("title", "臨江仙");
        model.addAttribute("content", contents[random.nextInt(6)]);
        return "show1";
    }

    @GetMapping(path = "show2")
    public String showTow(Map<String, Object> data) {
        data.put("name", "Show2---->");
        data.put("now", LocalDateTime.now().toString());
        return "show2";
    }
}

上面的三種case中

  • 第一個(gè)是最好理解的,在創(chuàng)建ModelAndView時(shí),傳入viewName和數(shù)據(jù)

  • 第二個(gè)是通過(guò)接口參數(shù)Model,設(shè)置傳遞給view的數(shù)據(jù)

  • 第三種則直接使用Map來(lái)傳遞數(shù)據(jù)

三個(gè)接口,對(duì)應(yīng)的三個(gè)html文件,如下

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="SpringBoot thymeleaf"/>
    <meta name="author" content="YiHui"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>YiHui's SpringBoot Demo</title>
    <link rel="stylesheet" href="index.css"/>
</head>
<body>

<div>
    <div class="title">hello world!</div>
    <br/>
    <div class="content" th:text="'歡迎訪問(wèn)' + ${name}">默認(rèn)的內(nèi)容</div>
    <br/>
    <div class="sign" th:text="'當(dāng)前時(shí)間' + ${now}">默認(rèn)的簽名</div>
    <br/>
    <a href="show1">傳參2測(cè)試</a> &nbsp;&nbsp;&nbsp;&nbsp;
    <a href="show2">傳參3測(cè)試</a>
</div>
</body>
</html>

show1.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="SpringBoot thymeleaf"/>
    <meta name="author" content="YiHui"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>YiHui's SpringBoot Demo</title>
    <link rel="stylesheet" href="index.css"/>
</head>
<body>

<div>
    <div class="title" th:text="${title}">標(biāo)題!</div>
    <div class="content" th:text="${content}">內(nèi)容</div>
</div>
</body>
</html>

show2.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="SpringBoot thymeleaf"/>
    <meta name="author" content="YiHui"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>YiHui's SpringBoot Demo</title>
    <link rel="stylesheet" href="index.css"/>
</head>
<body>

<div>
    <div class="title" th:text="${name}">標(biāo)題!</div>
    <div class="content" th:text="${now}">內(nèi)容</div>
</div>
</body>
</html>

在上面的模板文件中,需要注意引用css樣式文件,路徑前面并沒(méi)有static,我們對(duì)應(yīng)的css文件

index.css

.title {
    color: #c00;
    font-weight: normal;
    font-size: 2em;
}

.content {
    color: darkblue;
    font-size: 1.2em;
}

.sign {
    color: lightgray;
    font-size: 0.8em;
    font-style: italic;
}

3. 演示

啟動(dòng)項(xiàng)目后,可以看到三個(gè)頁(yè)面的切換,模板中的數(shù)據(jù)根據(jù)后端的返回替換,特別是主頁(yè)的時(shí)間,每次刷新都會(huì)隨之改變

SpringBoot中怎么搭建Thymeleaf環(huán)境

看完上述內(nèi)容,你們掌握SpringBoot中怎么搭建Thymeleaf環(huán)境的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI