此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf!! 之前写过一篇是springMVC与thymeleaf 的自定义标签(属于自定义方言的属性一
此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf!!
之前写过一篇是springMVC与thymeleaf 的自定义标签(属于自定义方言的属性一块,类似thy的th:if和th:text等)文章,如果你想了解,以下是地址:
点击>>Thymeleaf3.0自定义标签属性
这篇例子可以实现你的分页标签实现等功能,不会讲一堆的废话和底层的原理(自行百度),属于快速上手教程,请认真看以下内容!
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自定义标签的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。