欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot學習之Thymeleaf模板引擎及原理介紹

 更新時間:2022年02月24日 10:28:27   作者:我是坑貨  
本文主要介紹一下SpringBoot給我們推薦的Thymeleaf模板引擎,這模板引擎呢,是一個高級語言的模板引擎,他的這個語法更簡單而且功能更強大,對springboot?Thymeleaf模板引擎相關(guān)知識感興趣的朋友一起看看吧

模板引擎

springboot我們目前是以jar包的形式打包,實際上我們之前是打成war包,放到tomcat服務(wù)器里面,可以用JSP。
但是jar包就導(dǎo)致不能用JSP,換一種方式就是springboot推薦的Thymeleaf模板引擎(JSP也是一種模板引擎,除此之外還有什么framework也是一種模板引擎),

什么是模板引擎?

模板引擎就是解決我們需要動態(tài)賦值給前端的一種解決方案
(模板引擎的作用就是我們來寫一個頁面模板,比如有些值呢,是動態(tài)的,我們寫一些表達式。而這些值,從哪來呢,就是我們在后臺封裝一些數(shù)據(jù)。然后把這個模板和這個數(shù)據(jù)交給我們模板引擎,模板引擎按照我們這個數(shù)據(jù)幫你把這表達式解析、填充到我們指定的位置,然后把這個數(shù)據(jù)最終生成一個我們想要的內(nèi)容給我們寫出去,這就是我們這個模板引擎,不管是jsp還是其他模板引擎,都是這個思想。只不過呢,就是說不同模板引擎之間,他們可能這個語法有點不一樣。其他的我就不介紹了,我主要來介紹一下SpringBoot給我們推薦的Thymeleaf模板引擎,這模板引擎呢,是一個高級語言的模板引擎,他的這個語法更簡單。而且呢,功能更強大。)

模板引擎的原理

引入Thymeleaf

引入就是導(dǎo)入依賴,以下是網(wǎng)址是相關(guān)的地址
Thymeleaf 官網(wǎng):https://www.thymeleaf.org/

Thymeleaf 在Github 的主頁:https://github.com/thymeleaf/thymeleaf

Spring官方文檔:找到我們對應(yīng)的版本

https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter

找到對應(yīng)的pom依賴:可以適當點進源碼看下本來的包!

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

Thymeleaf分析

Thymeleaf的引入依賴文件ThymeleafProperties,文件內(nèi)容如下:

  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available
package org.springframework.boot.autoconfigure.thymeleaf;
@org.springframework.boot.context.properties.ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
    private static final java.nio.charset.Charset DEFAULT_ENCODING;
    public static final java.lang.String DEFAULT_PREFIX = "classpath:/templates/";
    public static final java.lang.String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate;
    private boolean checkTemplateLocation;
    private java.lang.String prefix;
    private java.lang.String suffix;
    private java.lang.String mode;
    private java.nio.charset.Charset encoding;
    private boolean cache;
    private java.lang.Integer templateResolverOrder;
    private java.lang.String[] viewNames;
    private java.lang.String[] excludedViewNames;
    private boolean enableSpringElCompiler;
    private boolean renderHiddenMarkersBeforeCheckboxes;
    private boolean enabled;
    private final org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Servlet servlet;
    private final org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Reactive reactive;
    public ThymeleafProperties() { /* compiled code */ }
    public boolean isEnabled() { /* compiled code */ }
    public void setEnabled(boolean enabled) { /* compiled code */ }
    public boolean isCheckTemplate() { /* compiled code */ }
    public void setCheckTemplate(boolean checkTemplate) { /* compiled code */ }
    public boolean isCheckTemplateLocation() { /* compiled code */ }
    public void setCheckTemplateLocation(boolean checkTemplateLocation) { /* compiled code */ }
    public java.lang.String getPrefix() { /* compiled code */ }
    public void setPrefix(java.lang.String prefix) { /* compiled code */ }
    public java.lang.String getSuffix() { /* compiled code */ }
    public void setSuffix(java.lang.String suffix) { /* compiled code */ }
    public java.lang.String getMode() { /* compiled code */ }
    public void setMode(java.lang.String mode) { /* compiled code */ }
    public java.nio.charset.Charset getEncoding() { /* compiled code */ }
    public void setEncoding(java.nio.charset.Charset encoding) { /* compiled code */ }
    public boolean isCache() { /* compiled code */ }
    public void setCache(boolean cache) { /* compiled code */ }
    public java.lang.Integer getTemplateResolverOrder() { /* compiled code */ }
    public void setTemplateResolverOrder(java.lang.Integer templateResolverOrder) { /* compiled code */ }
    public java.lang.String[] getExcludedViewNames() { /* compiled code */ }
    public void setExcludedViewNames(java.lang.String[] excludedViewNames) { /* compiled code */ }
    public java.lang.String[] getViewNames() { /* compiled code */ }
    public void setViewNames(java.lang.String[] viewNames) { /* compiled code */ }
    public boolean isEnableSpringElCompiler() { /* compiled code */ }
    public void setEnableSpringElCompiler(boolean enableSpringElCompiler) { /* compiled code */ }
    public boolean isRenderHiddenMarkersBeforeCheckboxes() { /* compiled code */ }
    public void setRenderHiddenMarkersBeforeCheckboxes(boolean renderHiddenMarkersBeforeCheckboxes) { /* compiled code */ }
    public org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Reactive getReactive() { /* compiled code */ }
    public org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties.Servlet getServlet() { /* compiled code */ }
    public static class Reactive {
        private org.springframework.util.unit.DataSize maxChunkSize;
        private java.util.List<org.springframework.http.MediaType> mediaTypes;
        private java.lang.String[] fullModeViewNames;
        private java.lang.String[] chunkedModeViewNames;
        public Reactive() { /* compiled code */ }
        public java.util.List<org.springframework.http.MediaType> getMediaTypes() { /* compiled code */ }
        public void setMediaTypes(java.util.List<org.springframework.http.MediaType> mediaTypes) { /* compiled code */ }
        public org.springframework.util.unit.DataSize getMaxChunkSize() { /* compiled code */ }
        public void setMaxChunkSize(org.springframework.util.unit.DataSize maxChunkSize) { /* compiled code */ }
        public java.lang.String[] getFullModeViewNames() { /* compiled code */ }
        public void setFullModeViewNames(java.lang.String[] fullModeViewNames) { /* compiled code */ }
        public java.lang.String[] getChunkedModeViewNames() { /* compiled code */ }
        public void setChunkedModeViewNames(java.lang.String[] chunkedModeViewNames) { /* compiled code */ }
    }
    public static class Servlet {
        private org.springframework.util.MimeType contentType;
        private boolean producePartialOutputWhileProcessing;
        public Servlet() { /* compiled code */ }
        public org.springframework.util.MimeType getContentType() { /* compiled code */ }
        public void setContentType(org.springframework.util.MimeType contentType) { /* compiled code */ }
        public boolean isProducePartialOutputWhileProcessing() { /* compiled code */ }
        public void setProducePartialOutputWhileProcessing(boolean producePartialOutputWhileProcessing) { /* compiled code */ }
}

我們可以在其中看到默認的前綴和后綴。
我們只需要把我們的html頁面放在類路徑下的templates下,thymeleaf就可以幫我們自動渲染了。

啟動訪問結(jié)果

Thymeleaf 語法學習

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

Thymeleaf 官網(wǎng):https://www.thymeleaf.org/ , 簡單看一下官網(wǎng)!我們?nèi)ハ螺dThymeleaf的官方文檔!

做一個最簡單的向頁面?zhèn)髦挡僮?br />(1)寫一個頁面,一個msg來作為傳值

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>ZP</title>
</head>
<body>
<h1>測試頁面</h1>

<!--th:text就是將div中的內(nèi)容設(shè)置為它指定的值,和之前學習的Vue一樣-->
<div th:text="${msg}"></div>
</body>
</html>

代碼
注意這里return的字符串要和上面的templates下面的html文件名字相同。

package com.example.zpspringboot3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @Author: zhangpeng
 * @Date: 2022/2/21 16:02
 */
@Controller
public class TestController {
//    @RequestMapping("/t1")
//    public String test1(){
//        //classpath:/templates/test.html
//        return "text";
//    }
    @RequestMapping("/t1")
    public String test1(Model model){
        //存入數(shù)據(jù)
        model.addAttribute("msg","Hello,Thymeleaf");
        //classpath:/templates/test.html
        return "text2";
    }
}

測試

通過thymeleaf官網(wǎng)文檔學習一些語法:
1、我們可以使用任意的 th:attr 來替換Html中原生屬性的值!

2、我們能寫哪些表達式呢?

Simple expressions:(表達式語法)
Variable Expressions: ${...}:獲取變量值;OGNL;
    1)、獲取對象的屬性、調(diào)用方法
    2)、使用內(nèi)置的基本對象:#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)置的一些工具對象:
      #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: *{...}:選擇表達式:和${}在功能上是一樣;
  Message Expressions: #{...}:獲取國際化內(nèi)容
  Link URL Expressions: @{...}:定義URL;
  Fragment Expressions: ~{...}:片段引用表達式
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ù)學運算)
    Binary operators: + , - , * , / , %
    Minus sign (unary operator): -
Boolean operations:(布爾運算)
    Binary operators: and , or
    Boolean negation (unary operator): ! , not
Comparisons and equality:(比較運算)
    Comparators: > , < , >= , <= ( gt , lt , ge , le )
    Equality operators: == , != ( eq , ne )
Conditional operators:條件運算(三元運算符)
    If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)
    Default: (value) ?: (defaultvalue)
Special tokens:
    No-Operation: _

練習測試:

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

 @RequestMapping("/t2")
    public String test2(Map<String,Object> map){
        //存入數(shù)據(jù)
        map.put("msg","<h1>Hello</h1>");
        map.put("users", Arrays.asList("zp","zhangpeng"));
        //classpath:/templates/test.html
        return "text3";
    }
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>狂神說</title>
</head>
<body>
<h1>測試頁面</h1>
<div th:text="${msg}"></div>
<!--不轉(zhuǎn)義-->
<div th:utext="${msg}"></div>
<!--遍歷數(shù)據(jù)-->
<!--th:each每次遍歷都會生成當前這個標簽:官網(wǎng)#9-->
<h4 th:each="user :${users}" th:text="${user}"></h4>
<h4>
    <!--行內(nèi)寫法:官網(wǎng)#12-->
    <span th:each="user:${users}">[[${user}]]</span>
</h4>
</body>
</html>

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

相關(guān)文章

  • IDEA2022中部署Tomcat Web項目的流程分析

    IDEA2022中部署Tomcat Web項目的流程分析

    這篇文章主要介紹了IDEA2022中部署Tomcat Web項目,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Spring mvc 實現(xiàn)用戶登錄的方法(攔截器)

    Spring mvc 實現(xiàn)用戶登錄的方法(攔截器)

    這篇文章主要介紹了Spring mvc 實現(xiàn)用戶登錄的方法(攔截器),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 詳解Java消息隊列-Spring整合ActiveMq

    詳解Java消息隊列-Spring整合ActiveMq

    本篇文章主要介紹了詳解Java消息隊列-Spring整合ActiveMq ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Mybatis常見注解有哪些(總結(jié))

    Mybatis常見注解有哪些(總結(jié))

    這篇文章主要介紹了Mybatis常見注解有哪些(總結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

    Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

    這篇文章主要介紹了Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • JWT在OpenFeign調(diào)用中進行令牌中繼詳解

    JWT在OpenFeign調(diào)用中進行令牌中繼詳解

    Feign是一個聲明式的Web Service客戶端,是一種聲明式、模板化的HTTP客戶端。而OpenFeign是Spring Cloud 在Feign的基礎(chǔ)上支持了Spring MVC的注解,如@RequesMapping等等,這篇文章主要給大家介紹了關(guān)于JWT在OpenFeign調(diào)用中進行令牌中繼的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • SpringBoot使用MyBatis-Plus解決Invalid?bound?statement異常

    SpringBoot使用MyBatis-Plus解決Invalid?bound?statement異常

    這篇文章主要介紹了SpringBoot使用MyBatis-Plus解決Invalid?bound?statement異常,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Java CompletableFuture的使用詳解

    Java CompletableFuture的使用詳解

    這篇文章主要介紹了Java CompletableFuture的使用詳解,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下
    2021-03-03
  • springMVC如何對輸入數(shù)據(jù)校驗實現(xiàn)代碼

    springMVC如何對輸入數(shù)據(jù)校驗實現(xiàn)代碼

    數(shù)據(jù)的校驗是交互式網(wǎng)站一個不可或缺的功能,數(shù)據(jù)驗證分為客戶端驗證和服務(wù)器端驗證,這篇文章主要介紹了springMVC如何對輸入數(shù)據(jù)校驗,需要的朋友可以參考下
    2020-10-10
  • Java的RocketMq隊列之消息可靠性詳解

    Java的RocketMq隊列之消息可靠性詳解

    這篇文章主要介紹了Java的RocketMq隊列之消息可靠性詳解,生產(chǎn)者通過網(wǎng)絡(luò)發(fā)送消息給 Broker,當 Broker 收到之后,將會返回確認響應(yīng)信息給 Producer,所以生產(chǎn)者只要接收到返回的確認響應(yīng),就代表消息在生產(chǎn)階段未丟失,需要的朋友可以參考下
    2024-01-01

最新評論