亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot超詳細(xì)講解Thymeleaf模板引擎

 更新時間:2022年07月18日 15:50:03   作者:dengfengling999  
這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下

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

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

B:瀏覽器:用來顯示數(shù)據(jù),發(fā)送請求,沒有處理能力

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

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

第一個例子:

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模板引擎");
        //指定視圖 模板引擎使用的頁面(html)
        //邏輯名稱
        return "hello";
    }
}

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

創(chuàng)建hello.html:

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

運(yùn)行主啟動類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爆紅,和寫的時候沒有提示信息

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

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h3>使用Thymeleaf的例子</h3>
   <!--使用模板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>

使用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ù)");
        //指定視圖 模板引擎使用的頁面(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>
   <h3>使用Thymeleaf的例子</h3>
   <!--使用模板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>

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

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

到此這篇關(guān)于SpringBoot超詳細(xì)講解Thymeleaf模板引擎的文章就介紹到這了,更多相關(guān)SpringBoot Thymeleaf模板引擎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot如何從數(shù)據(jù)庫獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化)

    springboot如何從數(shù)據(jù)庫獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化)

    這篇文章主要介紹了springboot如何從數(shù)據(jù)庫獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • JVM垃圾回收算法的概念與分析

    JVM垃圾回收算法的概念與分析

    這篇文章主要給大家介紹了關(guān)于JVM垃圾回收算法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用JVM具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • springboot如何配置定時任務(wù)

    springboot如何配置定時任務(wù)

    這篇文章主要介紹了springboot如何配置定時任務(wù),幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • Tomcat中session的管理機(jī)制

    Tomcat中session的管理機(jī)制

    這篇文章主要為大家詳細(xì)介紹了Tomcat中session的管理機(jī)制 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 如何使用JFrame完成動態(tài)模擬時鐘

    如何使用JFrame完成動態(tài)模擬時鐘

    本文介紹了如何使用JFrame完成動態(tài)模擬時鐘,需要的朋友可以參考下
    2015-08-08
  • java selenium教程環(huán)境搭建方法

    java selenium教程環(huán)境搭建方法

    本文主要介紹java selenium 環(huán)境搭建,這里詳細(xì)介紹了selenium的安裝環(huán)境搭建,有興趣的小伙伴可以參考下
    2016-08-08
  • netty-grpc一次DirectByteBuffer內(nèi)存泄露問題

    netty-grpc一次DirectByteBuffer內(nèi)存泄露問題

    這篇文章主要介紹了netty-grpc一次DirectByteBuffer內(nèi)存泄露問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java中Calendar日期類常用方法演示

    Java中Calendar日期類常用方法演示

    這篇文章主要給大家介紹了關(guān)于Java中Calendar日期類用法詳細(xì)介紹的相關(guān)資料,Calendar類是?Java?中用于處理日期和時間的抽象類,它提供了一種獨(dú)立于特定日歷系統(tǒng)的方式來處理日期和時間,需要的朋友可以參考下
    2023-12-12
  • javaweb中Filter(過濾器)的常見應(yīng)用

    javaweb中Filter(過濾器)的常見應(yīng)用

    這篇文章主要介紹了javaweb中Filter的常見應(yīng)用,過濾器的使用方法,感興趣的小伙伴們可以參考一下
    2015-12-12
  • java實(shí)現(xiàn)掃雷游戲入門程序

    java實(shí)現(xiàn)掃雷游戲入門程序

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)掃雷游戲入門程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06

最新評論