溫馨提示×

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

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

SpringBoot之Thymeleaf模板引擎實(shí)例分析

發(fā)布時(shí)間:2022-07-19 09:48:33 來(lái)源:億速云 閱讀:122 作者:iii 欄目:開(kāi)發(fā)技術(shù)

今天小編給大家分享一下SpringBoot之Thymeleaf模板引擎實(shí)例分析的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

Jsp是最早的模板技術(shù),用來(lái)處理視圖層的,用來(lái)做數(shù)據(jù)顯示的模板

SpringBoot之Thymeleaf模板引擎實(shí)例分析

B S結(jié)構(gòu):

B:瀏覽器:用來(lái)顯示數(shù)據(jù),發(fā)送請(qǐng)求,沒(méi)有處理能力

發(fā)送一個(gè)請(qǐng)求,訪問(wèn)a.jsp,a.jsp在服務(wù)器端變成Servlet,在將輸出的數(shù)據(jù)返回給瀏覽器,瀏覽器就可以看到結(jié)果數(shù)據(jù),jsp最終翻譯過(guò)來(lái)也是個(gè)html頁(yè)面

模板技術(shù)你就可以把它們當(dāng)成字符串的替換,比如說(shuō):這里{data}這里有一個(gè)字符串,你把它換成固定值其他值,但是這個(gè)替換有一些附加的功能,通過(guò)模板技術(shù)處理視圖層的內(nèi)容

SpringBoot之Thymeleaf模板引擎實(shí)例分析

第一個(gè)例子:

SpringBoot之Thymeleaf模板引擎實(shí)例分析

pom.xml: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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>027-thymeleaf-first</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--模板引擎起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--web起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

創(chuàng)建Controller控制器:HelloThymeleafController:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(HttpServletRequest request){
        //添加數(shù)據(jù)到request作用域,模板引擎可以從request中獲取數(shù)據(jù)
        request.setAttribute("data","歡迎使用Thymeleaf模板引擎");
        //指定視圖 模板引擎使用的頁(yè)面(html)
        //邏輯名稱
        return "hello";
    }
}

templates:用來(lái)放模板用到的視圖文件的,模板引擎用到的模板到放在了template目錄之下:

創(chuàng)建hello.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h4>使用Thymeleaf的例子</h4>
   <!--使用模板th:text=""獲取數(shù)據(jù)-->
   <p th:text="${data}">想顯示數(shù)據(jù)</p>
</body>
</html>

運(yùn)行主啟動(dòng)類Application:

package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

可以在hello.html中加入:未解決在標(biāo)簽中th爆紅,和寫的時(shí)候沒(méi)有提示信息

xmlns:th="http://www.thymeleaf.org" 在標(biāo)簽中,再次寫th的時(shí)候就有提示記錄了

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h4>使用Thymeleaf的例子</h4>
   <!--使用模板th:text="${data}"獲取后端request的作用域中的數(shù)據(jù),把data數(shù)據(jù)替換文本,text表示取數(shù)據(jù)-->
   <p th:text="${data}">想顯示數(shù)據(jù)</p>
   <p th:text="${data}">顯示</p>
</body>
</html>

SpringBoot之Thymeleaf模板引擎實(shí)例分析

使用Model:

在Controller:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(Model model, HttpServletRequest request){
        //添加數(shù)據(jù)到request作用域,模板引擎可以從request中獲取數(shù)據(jù)
        request.setAttribute("data","歡迎使用Thymeleaf模板引擎");
        //使用model和request作用域是一樣的 實(shí)際上model中的數(shù)據(jù)就是放到request作用域中的
        model.addAttribute("mydata","model中的數(shù)據(jù)");
        //指定視圖 模板引擎使用的頁(yè)面(html)
        //邏輯名稱
        return "hello";
    }
}

hello.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h4>使用Thymeleaf的例子</h4>
   <!--使用模板th:text="${data}"獲取后端request的作用域中的數(shù)據(jù),把data數(shù)據(jù)替換文本,text表示取數(shù)據(jù)-->
   <p th:text="${data}">想顯示數(shù)據(jù)</p>
   <p th:text="${mydata}">顯示</p>
</body>
</html>

SpringBoot之Thymeleaf模板引擎實(shí)例分析

speingboot配置文件application.properties:模板引擎的常用相關(guān)設(shè)置,基本不用設(shè)置都是默認(rèn)的:

#在開(kāi)發(fā)階段,關(guān)閉模板發(fā)緩存,讓修改立即生效 設(shè)置為true時(shí),就是使用模板緩存,當(dāng)?shù)诙卧L問(wèn)的時(shí)候,使用內(nèi)存中的數(shù)據(jù),就不在解析模板了
spring.thymeleaf.cache=false
#編碼格式
spring.thymeleaf.encoding=UTF-8
#模板的類型(默認(rèn)是 html,模板是html文件 它不僅支持網(wǎng)頁(yè)做模板還支持其他類別的)
spring.thymeleaf.model=HTML
#模板的前綴:默認(rèn)是類路徑的classpath:/templates目錄下
spring.thymeleaf.prefix=classpath:/templates/
#后綴
spring.thymeleaf.suffix=.html

以上就是“SpringBoot之Thymeleaf模板引擎實(shí)例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(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