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

SpringBoot使用Thymeleaf自定義標簽的實例代碼

 更新時間:2020年09月18日 13:33:25   作者:Garc  
這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標簽的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

此篇文章內(nèi)容僅限于 描述springboot與 thy 自定義標簽的說明,所以你在看之前,請先會使用springboot和thymeleaf?。?/strong>

之前寫過一篇是springMVC與thymeleaf 的自定義標簽(屬于自定義方言的屬性一塊,類似thy的th:if和th:text等)文章,如果你想了解,以下是地址:

點擊>>Thymeleaf3.0自定義標簽屬性

這篇例子可以實現(xiàn)你的分頁標簽實現(xiàn)等功能,不會講一堆的廢話和底層的原理(自行百度),屬于快速上手教程,請認真看以下內(nèi)容!

PS: 請允許我將thymeleaf簡稱thy,springboot簡稱sb

依然直奔主題,sb本身是自帶thy的,而且使用方式也很簡單,直接配置application.properties 這個文件就可以了,當(dāng)然你不配也是可以的。但是,需要配置自定義方言的話,就需要自己把配置重新寫出來,看下面代碼:

說明:RiskDialect是我自己的自定義標簽,而且從這個配置可以簡單看出,spring視圖的配置通過注解的方式將thymeleaf配置進去了

@Configuration
public class TemplateEngineConfig{
 
 @Bean
 public ContentNegotiatingViewResolver getViewResolver(){
  ServletContextTemplateResolver templateResolver=new ServletContextTemplateResolver();
  templateResolver.setPrefix("/WEB-INF/views/");
  templateResolver.setSuffix(".html");
  templateResolver.setTemplateMode("HTML5");
  templateResolver.setCacheable(false);
  templateResolver.setCharacterEncoding("UTF-8");
  Set<IDialect> additionalDialects=new LinkedHashSet<IDialect>();
  //自定義方言
  additionalDialects.add(new RiskDialect());
  SpringTemplateEngine templateEngine = new SpringTemplateEngine();
  templateEngine.setAdditionalDialects(additionalDialects);
  templateEngine.setTemplateResolver(templateResolver);
  ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
  thymeleafViewResolver.setTemplateEngine(templateEngine);
  thymeleafViewResolver.setCharacterEncoding("UTF-8");
  thymeleafViewResolver.setOrder(1);
  List<ViewResolver> viewResolvers= new ArrayList<>();
  viewResolvers.add(thymeleafViewResolver);
  ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
  viewResolver.setViewResolvers(viewResolvers);
  return viewResolver;
 }
}

接下來看RiskDialect實現(xiàn):

說明:SanstitvEncryptProcessor這個類是 thymeleaf處理器,用來處理定義方言邏輯的

package com.garc.thymeleaf.dialect;
 
import org.springframework.stereotype.Component;
import org.thymeleaf.dialect.AbstractDialect;
import org.thymeleaf.dialect.AbstractXHTMLEnabledDialect;
import org.thymeleaf.processor.IProcessor;
 
import java.util.HashSet;
import java.util.Set;
 
/**
 * Created by Garc on 2018/1/17.
 */
public class RiskDialect extends AbstractDialect {
 
 private static final String PREFIX="risk";
 private static final String ELEMENT_NAME="sanstitv";
 
 @Override
 public String getPrefix() {
  return PREFIX;
 }
 
 @Override
 public Set<IProcessor> getProcessors() {
  final Set<IProcessor> processors = new HashSet<>();
  processors.add(new SanstitvEncryptProcessor(ELEMENT_NAME));
  return processors;
 }
}

繼續(xù)看SanstitvEncryptProcessor這個類:

package com.garc.thymeleaf.dialect;
 
import org.springframework.context.ApplicationContext;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Node;
import org.thymeleaf.dom.Text;
import org.thymeleaf.processor.ProcessorResult;
import org.thymeleaf.processor.element.AbstractElementProcessor;
import org.thymeleaf.processor.element.AbstractMarkupSubstitutionElementProcessor;
import org.thymeleaf.spring4.context.SpringWebContext;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by Garc on 2018/1/17.
 */
public class SanstitvEncryptProcessor extends AbstractMarkupSubstitutionElementProcessor {
 
 protected SanstitvEncryptProcessor(String elementName) {
  super(elementName);
 }
 
 @Override
 protected List<Node> getMarkupSubstitutes(Arguments arguments, Element element) {
  final Element container = new Element("div");
  final Text text = new Text("是的,這是測試");
  container.addChild(text);
 
  final List<Node> nodes = new ArrayList<>();
  nodes.add(container);
  return nodes;
 }
 
 @Override
 public int getPrecedence() {
  return 1000;
 } 
}

html使用方式:

risk:sanstitv 是我自定義用的標簽

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  xmlns:risk="http://www.w3.org/1999/xhtml">
 <head>
  <meta content="text/html;charset=UTF-8"></meta>
  <title>Title</title>
 </head>
 <body>
  <span th:text="${test}"></span>
  <risk:sanstitv path="測試"></risk:sanstitv>
 </body>
</html>

以上這篇SpringBoot使用Thymeleaf自定義標簽的實例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Quartz+Spring Boot實現(xiàn)動態(tài)管理定時任務(wù)

    Quartz+Spring Boot實現(xiàn)動態(tài)管理定時任務(wù)

    最近做項目遇到動態(tài)管理定時任務(wù)的需求,剛拿到這個需求還真不知道從哪下手,經(jīng)過一番思考,終于找出實現(xiàn)思路,接下來通過本文給大家介紹了Quartz+Spring Boot實現(xiàn)動態(tài)管理定時任務(wù)的相關(guān)知識,需要的朋友可以參考下
    2018-09-09
  • java并發(fā)訪問重復(fù)請求過濾問題

    java并發(fā)訪問重復(fù)請求過濾問題

    本篇文章給大家分享了關(guān)于java并發(fā)訪問重復(fù)請求過濾的相關(guān)問題以及解決方法,對此有需要的朋友參考學(xué)習(xí)下。
    2018-05-05
  • 遠程debug調(diào)試入門

    遠程debug調(diào)試入門

    這篇文章主要介紹了Eclipse的Debug調(diào)試技巧大全(總結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧嗎,希望能給你帶來幫助
    2021-06-06
  • java如何實現(xiàn)post請求webservice服務(wù)端

    java如何實現(xiàn)post請求webservice服務(wù)端

    這篇文章主要介紹了java如何實現(xiàn)post請求webservice服務(wù)端,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java多線程并發(fā)FutureTask使用詳解

    Java多線程并發(fā)FutureTask使用詳解

    Java?的多線程機制本質(zhì)上能夠完成兩件事情,異步計算和并發(fā),F(xiàn)utureTask?是基于?Runnable?實現(xiàn)的一個可取消的異步調(diào)用?API,本文給大家介紹Java?多線程并發(fā)FutureTask及基本使用,需要的朋友可以參考下
    2022-06-06
  • 使用springboot 打包插件去除jar包瘦身

    使用springboot 打包插件去除jar包瘦身

    這篇文章主要介紹了使用springboot 打包插件去除jar包瘦身的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 快速解決commons-fileupload組件無法處理自定義head信息的bug

    快速解決commons-fileupload組件無法處理自定義head信息的bug

    問題在于fileupload組件解析完自定義的head節(jié)點后,卻忘記傳遞到FileItemStreamImpl中了,稍作修訂,即可修正該bug
    2013-08-08
  • SpringCloud客戶端的負載均衡Ribbon的實現(xiàn)

    SpringCloud客戶端的負載均衡Ribbon的實現(xiàn)

    微服務(wù)架構(gòu),不可避免的存在單個微服務(wù)有多個實例,這篇文章主要介紹了SpringCloud客戶端的負載均衡Ribbon的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 簡單了解Spring Framework5.0新特性

    簡單了解Spring Framework5.0新特性

    這篇文章主要介紹了簡單了解Spring Framework5.0新特性,涉及了核心框架修訂,核心容器更新,使用Kotlin進行函數(shù)式編程等幾個方面的介紹,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java 創(chuàng)建兩個線程模擬對話并交替輸出實現(xiàn)解析

    Java 創(chuàng)建兩個線程模擬對話并交替輸出實現(xiàn)解析

    這篇文章主要介紹了Java 創(chuàng)建兩個線程模擬對話并交替輸出實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10

最新評論