SpringBoot使用Thymeleaf自定义标签的实例代码
程序员文章站
2022-03-02 20:45:20
此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf!!之前写过一篇是springmvc与thymeleaf 的...
此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf!!
之前写过一篇是springmvc与thymeleaf 的自定义标签(属于自定义方言的属性一块,类似thy的th:if和th:text等)文章,如果你想了解,以下是地址:
这篇例子可以实现你的分页标签实现等功能,不会讲一堆的废话和底层的原理(自行百度),属于快速上手教程,请认真看以下内容!
ps: 请允许我将thymeleaf简称thy,springboot简称sb
依然直奔主题,sb本身是自带thy的,而且使用方式也很简单,直接配置application.properties 这个文件就可以了,当然你不配也是可以的。但是,需要配置自定义方言的话,就需要自己把配置重新写出来,看下面代码:
说明: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实现:
说明: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; } }
继续看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自定义标签的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。