国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot Thymeleaf模板引擎

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot Thymeleaf模板引擎。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Thymeleaf

模板引擎

  • 前端交給我們的頁(yè)面,是html頁(yè)面。如果是我們以前開(kāi)發(fā),我們需要把他們轉(zhuǎn)成jsp頁(yè)面,jsp好處就是當(dāng)我們查出一些數(shù)據(jù)轉(zhuǎn)發(fā)到JSP頁(yè)面以后,我們可以用jsp輕松實(shí)現(xiàn)數(shù)據(jù)的顯示,及交互等。

  • jsp支持非常強(qiáng)大的功能,包括能寫(xiě)Java代碼,但是呢,我們現(xiàn)在的這種情況,SpringBoot這個(gè)項(xiàng)目首先是以jar的方式,不是war,像第二,我們用的還是嵌入式的Tomcat,所以呢,他現(xiàn)在默認(rèn)是不支持jsp的。

  • 那不支持jsp,如果我們直接用純靜態(tài)頁(yè)面的方式,那給我們開(kāi)發(fā)會(huì)帶來(lái)非常大的麻煩,那怎么辦呢?

SpringBoot推薦你可以來(lái)使用模板引擎:

模板引擎,我們其實(shí)大家聽(tīng)到很多,其實(shí)jsp就是一個(gè)模板引擎,還有用的比較多的freemarker,包括SpringBoot給我們推薦的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他們的思想都是一樣的,什么樣一個(gè)思想呢我們來(lái)看一下這張圖:

SpringBoot Thymeleaf模板引擎,SpringBoot,spring boot,后端,java

模板引擎的作用就是我們來(lái)寫(xiě)一個(gè)頁(yè)面模板,比如有些值呢,是動(dòng)態(tài)的,我們寫(xiě)一些表達(dá)式。而這些值,從哪來(lái)呢,就是我們?cè)诤笈_(tái)封裝一些數(shù)據(jù)。然后把這個(gè)模板和這個(gè)數(shù)據(jù)交給我們模板引擎,模板引擎按照我們這個(gè)數(shù)據(jù)幫你把這表達(dá)式解析、填充到我們指定的位置,然后把這個(gè)數(shù)據(jù)最終生成一個(gè)我們想要的內(nèi)容給我們寫(xiě)出去,這就是我們這個(gè)模板引擎,不管是jsp還是其他模板引擎,都是這個(gè)思想。只不過(guò)呢,就是說(shuō)不同模板引擎之間,他們可能這個(gè)語(yǔ)法有點(diǎn)不一樣。其他的我就不介紹了,我主要來(lái)介紹一下SpringBoot給我們推薦的Thymeleaf模板引擎,這模板引擎呢,是一個(gè)高級(jí)語(yǔ)言的模板引擎,他的這個(gè)語(yǔ)法更簡(jiǎn)單。而且呢,功能更強(qiáng)大。

引入Thymeleaf

怎么引入呢,對(duì)于springboot來(lái)說(shuō),什么事情不都是一個(gè)start的事情嘛,我們?nèi)ピ陧?xiàng)目中引入一下。給大家三個(gè)網(wǎng)址:

  • Thymeleaf 官網(wǎng):https://www.thymeleaf.org/

  • Thymeleaf 在Github 的主頁(yè):https://github.com/thymeleaf/thymeleaf

  • Spring官方文檔:找到我們對(duì)應(yīng)的版本https://docs.spring.io/spring-boot/docs/2.6.5/reference/htmlsingle/#using-boot-starter

找到對(duì)應(yīng)的pom依賴(lài):可以適當(dāng)點(diǎn)進(jìn)源碼看下本來(lái)的包!

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

Thymeleaf分析

前面呢,我們已經(jīng)引入了Thymeleaf,那這個(gè)要怎么使用呢?

我們首先得按照SpringBoot的自動(dòng)配置原理看一下我們這個(gè)Thymeleaf的自動(dòng)配置規(guī)則,在按照那個(gè)規(guī)則,我們進(jìn)行使用。

我們?nèi)フ乙幌耇hymeleaf的自動(dòng)配置類(lèi):ThymeleafProperties

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
}

我們可以在其中看到默認(rèn)的前綴后綴
我們只需要把我們的html頁(yè)面放在類(lèi)路徑下的templates下,thymeleaf就可以幫我們自動(dòng)渲染了。
使用thymeleaf什么都不需要配置,只需要將他放在指定的文件夾下即可!

測(cè)試

  1. 編寫(xiě)一個(gè)TestController

    @Controller
    public class TestController {
        
        @RequestMapping("/test")
        public String test1(){
            //classpath:/templates/test.html
            return "test";
        }
        
    }
    
  2. 編寫(xiě)一個(gè)測(cè)試頁(yè)面 test.html 放在 templates 目錄下

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>Test頁(yè)面</h1>
    </body>
    </html>
    
  3. 啟動(dòng)項(xiàng)目請(qǐng)求測(cè)試

SpringBoot Thymeleaf模板引擎,SpringBoot,spring boot,后端,java

Thymeleaf 語(yǔ)法學(xué)習(xí)

要學(xué)習(xí)語(yǔ)法,還是參考官網(wǎng)文檔最為準(zhǔn)確,我們找到對(duì)應(yīng)的版本看一下;

Thymeleaf 官網(wǎng):https://www.thymeleaf.org/ , 簡(jiǎn)單看一下官網(wǎng)!我們?nèi)ハ螺dThymeleaf的官方文檔!在線(xiàn)文檔:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

Thymeleaf入門(mén)

我們做個(gè)最簡(jiǎn)單的練習(xí) :我們需要查出一些數(shù)據(jù),在頁(yè)面中展示

  1. 修改測(cè)試請(qǐng)求,增加數(shù)據(jù)傳輸;

    @RequestMapping("/t1")
    public String test1(Model model){
        //存入數(shù)據(jù)
        model.addAttribute("msg","Hello,Thymeleaf");
        //classpath:/templates/test.html
        return "test";
    }
    
  2. 我們要使用thymeleaf,需要在html文件中導(dǎo)入命名空間的約束,方便提示。

    我們可以去官方文檔中看一下命名空間拿來(lái)過(guò)來(lái):

 xmlns:th="http://www.thymeleaf.org"

SpringBoot Thymeleaf模板引擎,SpringBoot,spring boot,后端,java

  1. 我們?nèi)ゾ帉?xiě)下前端頁(yè)面

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>測(cè)試</title>
    </head>
    <body>
    <h1>測(cè)試頁(yè)面</h1>
    
    <!--th:text就是將div中的內(nèi)容設(shè)置為它指定的值,和之前學(xué)習(xí)的Vue一樣-->
    <div th:text="${msg}"></div>
    </body>
    </html>
    
  2. 啟動(dòng)測(cè)試!

    SpringBoot Thymeleaf模板引擎,SpringBoot,spring boot,后端,java

Thymeleaf語(yǔ)法

1、我們可以使用任意的 th:attr 來(lái)替換Html中原生屬性的值!

2、我們能寫(xiě)哪些表達(dá)式呢?

Simple expressions:(表達(dá)式語(yǔ)法)
Variable Expressions: ${...}:獲取變量值;OGNL;
    1)、獲取對(duì)象的屬性、調(diào)用方法
    2)、使用內(nèi)置的基本對(duì)象:#18
         #ctx : the context object.
         #vars: the context variables.
         #locale : the context locale.
         #request : (only in Web Contexts) the HttpServletRequest object.
         #response : (only in Web Contexts) the HttpServletResponse object.
         #session : (only in Web Contexts) the HttpSession object.
         #servletContext : (only in Web Contexts) the ServletContext object.

    3)、內(nèi)置的一些工具對(duì)象:
      #execInfo : information about the template being processed.
      #uris : methods for escaping parts of URLs/URIs
      #conversions : methods for executing the configured conversion service (if any).
      #dates : methods for java.util.Date objects: formatting, component extraction, etc.
      #calendars : analogous to #dates , but for java.util.Calendar objects.
      #numbers : methods for formatting numeric objects.
      #strings : methods for String objects: contains, startsWith, prepending/appending, etc.
      #objects : methods for objects in general.
      #bools : methods for boolean evaluation.
      #arrays : methods for arrays.
      #lists : methods for lists.
      #sets : methods for sets.
      #maps : methods for maps.
      #aggregates : methods for creating aggregates on arrays or collections.
==================================================================================

  Selection Variable Expressions: *{...}:選擇表達(dá)式:和${}在功能上是一樣;
  Message Expressions: #{...}:獲取國(guó)際化內(nèi)容
  Link URL Expressions: @{...}:定義URL;
  Fragment Expressions: ~{...}:片段引用表達(dá)式

Literals(字面量)
      Text literals: 'one text' , 'Another one!' ,…
      Number literals: 0 , 34 , 3.0 , 12.3 ,…
      Boolean literals: true , false
      Null literal: null
      Literal tokens: one , sometext , main ,…
      
Text operations:(文本操作)
    String concatenation: +
    Literal substitutions: |The name is ${name}|
    
Arithmetic operations:(數(shù)學(xué)運(yùn)算)
    Binary operators: + , - , * , / , %
    Minus sign (unary operator): -
    
Boolean operations:(布爾運(yùn)算)
    Binary operators: and , or
    Boolean negation (unary operator): ! , not
    
Comparisons and equality:(比較運(yùn)算)
    Comparators: > , < , >= , <= ( gt , lt , ge , le )
    Equality operators: == , != ( eq , ne )
    
Conditional operators:條件運(yùn)算(三元運(yùn)算符)
    If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)
    Default: (value) ?: (defaultvalue)
    
Special tokens:
    No-Operation: _

練習(xí)測(cè)試:

1、 我們編寫(xiě)一個(gè)Controller,放一些數(shù)據(jù)


@RequestMapping("/test2")
public String test2(Map<String,Object> map){
    //存入數(shù)據(jù)
    map.put("msg","<h1>Hello</h1>");
    map.put("users", Arrays.asList("qinjiang","kuangshen"));
    //classpath:/templates/test.html
    return "test";
}

2、測(cè)試頁(yè)面取出數(shù)據(jù)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>Test頁(yè)面</h1>
    <!--不轉(zhuǎn)義-->
    <div th:text="${msg}"></div>
    <!--轉(zhuǎn)義-->
    <div th:utext="${msg}"></div>

    <hr>
	<!--遍歷數(shù)據(jù)-->
	<!--th:each每次遍歷都會(huì)生成當(dāng)前這個(gè)標(biāo)簽:官網(wǎng)#9-->
    <h3 th:each="user:${users}" th:text="${user}"></h3>
    <hr>
     <!--行內(nèi)寫(xiě)法:官網(wǎng)#12-->
    <h3 th:each="user:${users}">[[ ${user} ]]</h3>
</div>
</body>
</html>

3、啟動(dòng)項(xiàng)目測(cè)試!

SpringBoot Thymeleaf模板引擎,SpringBoot,spring boot,后端,java

我們看完語(yǔ)法,很多樣式,我們即使現(xiàn)在學(xué)習(xí)了,也會(huì)忘記,所以我們?cè)趯W(xué)習(xí)過(guò)程中,需要使用什么,根據(jù)官方文檔來(lái)查詢(xún),才是最重要的,要熟練使用官方文檔!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-647326.html

到了這里,關(guān)于SpringBoot Thymeleaf模板引擎的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【SpringBoot學(xué)習(xí)筆記】04. Thymeleaf模板引擎

    【SpringBoot學(xué)習(xí)筆記】04. Thymeleaf模板引擎

    ?所有的html元素都可以被thymeleaf替換接管? th:元素名 templates下的只能通過(guò)Controller來(lái)跳轉(zhuǎn),templates前后端分離,需要模板引擎thymeleaf支持 ?? 模板引擎的作用就是我們來(lái)寫(xiě)一個(gè)頁(yè)面模板,比如有些值呢,是動(dòng)態(tài)的,我們寫(xiě)一些表達(dá)式。而這些值,從哪來(lái)呢,就是我們?cè)诤笈_(tái)封

    2024年02月13日
    瀏覽(21)
  • SpringBoot自帶模板引擎Thymeleaf使用詳解②

    SpringBoot自帶模板引擎Thymeleaf使用詳解②

    目錄 一、條件判斷和迭代遍歷 1.1 條件判斷 2.2 迭代遍歷 二、獲取域中的數(shù)據(jù)和URL寫(xiě)法 2.1 獲取域中的數(shù)據(jù) 2.2 URL寫(xiě)法 三、相關(guān)配置 語(yǔ)法 作用 th:if 條件判斷 準(zhǔn)備數(shù)據(jù) model.addAttribute(\\\"sex\\\",\\\"男\(zhòng)\\"); 使用實(shí)例 div ??? span th:if=\\\"${sex}==\\\'女\\\'\\\"這是女生/span ??? span th:if=\\\"${sex}==\\\'男\(zhòng)\\'\\\"這是男

    2024年02月08日
    瀏覽(18)
  • SpringBoot自帶模板引擎Thymeleaf使用詳解①

    SpringBoot自帶模板引擎Thymeleaf使用詳解①

    目錄 前言 一、SpringBoot靜態(tài)資源相關(guān)目錄 二、變量輸出 2.1 在templates目錄下創(chuàng)建視圖index.html 2.2 創(chuàng)建對(duì)應(yīng)的Controller 2.3 在視圖展示model中的值 三、操作字符串和時(shí)間 3.1 操作字符串 3.2 操作時(shí)間 ????????Thymeleaf是一款用于渲染XML/HTML5內(nèi)容的模板引擎,類(lèi)似JSP。它可以輕易的

    2024年02月08日
    瀏覽(20)
  • 【Springboot】SpringBoot基礎(chǔ)知識(shí)及整合Thymeleaf模板引擎

    【Springboot】SpringBoot基礎(chǔ)知識(shí)及整合Thymeleaf模板引擎

    ??博客x主頁(yè):己不由心王道長(zhǎng)??! ??文章說(shuō)明:spring?? ?系列專(zhuān)欄:spring ??本篇內(nèi)容:對(duì)SpringBoot進(jìn)行一個(gè)入門(mén)學(xué)習(xí)及對(duì)Thymeleaf模板引擎進(jìn)行整合(對(duì)所需知識(shí)點(diǎn)進(jìn)行選擇閱讀呀~)?? ??每日一語(yǔ):在人生的道路上,即使一切都失去了,只要一息尚存,你就沒(méi)有絲毫理

    2023年04月23日
    瀏覽(25)
  • 15 springboot項(xiàng)目——thymeleaf語(yǔ)法與關(guān)閉模板引擎

    15 springboot項(xiàng)目——thymeleaf語(yǔ)法與關(guān)閉模板引擎

    ? ? ? ? 在html文件中,有些是需要使用本地的css樣式,使用thymeleaf語(yǔ)法加載: ? ? ? ? 首先對(duì)head標(biāo)簽上面的html標(biāo)簽進(jìn)行更改: ? ? ? ? 其次,導(dǎo)入thymeleaf依賴(lài): ? ? ? ? 接著,使用thymeleaf語(yǔ)法: ? ? ? ? 碰到href或者src后邊與靜態(tài)資源有關(guān)的的本地路徑要進(jìn)行修改,把要

    2024年02月14日
    瀏覽(18)
  • 使用 Velocity 模板引擎的 Spring Boot 應(yīng)用

    使用 Velocity 模板引擎的 Spring Boot 應(yīng)用

    模板引擎是構(gòu)建動(dòng)態(tài)內(nèi)容的重要工具,特別適用于生成HTML、郵件內(nèi)容、報(bào)告和其他文本文檔。Velocity是一個(gè)強(qiáng)大的模板引擎,它具有簡(jiǎn)單易用的語(yǔ)法和靈活性。本文將介紹如何在Spring Boot應(yīng)用中使用Velocity模板引擎,并提供示例代碼。 Velocity是一個(gè)用于生成文本輸出的模板引擎

    2024年02月07日
    瀏覽(53)
  • thymeleaf模板引擎

    thymeleaf模板引擎

    ThymeleafProperties 配置類(lèi) 1.默認(rèn)編碼 2.前綴 3.后綴 相當(dāng)于視圖解析器? ? 這是學(xué)SpringBoot的必經(jīng)之路,非常重要?。。。ǔ悄闶菍W(xué)前端的) ? 只改了前端代碼點(diǎn)一下這個(gè)就可以刷新? ? 傳值過(guò)來(lái)了? th:text=\\\"${msg}\\\"爆紅,但是可以顯示,F(xiàn)ile-Settings-Editor-Inspection ?取消“Expression

    2024年02月14日
    瀏覽(20)
  • SpringBoot+Thymeleaf 后端轉(zhuǎn)html,pdf HTML生成PDF SpringBoot生成PDF Java PDF生成

    SpringBoot+Thymeleaf 后端轉(zhuǎn)html,pdf HTML生成PDF SpringBoot生成PDF Java PDF生成

    本文詳細(xì)介紹了如何使用SpringBoot和Thymeleaf將后端HTML轉(zhuǎn)換為PDF,包括依賴(lài)介紹、模板渲染以及PDF生成等步驟。

    2024年02月09日
    瀏覽(69)
  • 前端模板引擎Thymeleaf的整合和使用

    目錄 一、添加依賴(lài) 1.1首先,在項(xiàng)目的構(gòu)建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依賴(lài)。例如,對(duì)于 Maven 項(xiàng)目,在 pom.xml 文件中添加以下依賴(lài) 1.2保存并更新項(xiàng)目依賴(lài) 二、配置Thymeleaf 2.1模板位置配置 2.2模板緩存配置 2.3自定義標(biāo)簽配置 三、創(chuàng)建模板文件 3.1創(chuàng)建一個(gè)HTML文

    2024年04月27日
    瀏覽(32)
  • 【Spring Boot+Thymeleaf+MyBatis+mysql】實(shí)現(xiàn)電子商務(wù)平臺(tái)實(shí)戰(zhàn)(附源碼)持續(xù)更新~~ 包括sql語(yǔ)句、java、html代碼

    【Spring Boot+Thymeleaf+MyBatis+mysql】實(shí)現(xiàn)電子商務(wù)平臺(tái)實(shí)戰(zhàn)(附源碼)持續(xù)更新~~ 包括sql語(yǔ)句、java、html代碼

    源碼請(qǐng)點(diǎn)贊關(guān)注收藏后評(píng)論區(qū)留言和私信博主 開(kāi)發(fā)環(huán)境:Web服務(wù)器使用Servlet容器,數(shù)據(jù)庫(kù)采用mysql,集成開(kāi)發(fā)環(huán)境為Spring Tool Suite(STS) 電子商務(wù)平臺(tái)分為兩個(gè)子系統(tǒng) 一個(gè)是后臺(tái)管理系統(tǒng) 一個(gè)是電子商務(wù)系統(tǒng),下面分別講解著兩個(gè)子系統(tǒng)的功能需要與模塊劃分 1:后臺(tái)管理子

    2024年02月09日
    瀏覽(23)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包