一聚教程网:一个值得你收藏的教程网站

最新下载

SpringBoot使用Thymeleaf自定义标签代码实例

时间:2020-09-18 16:35:28 编辑:袖梨 来源:转载

本篇文章小编给大家分享一下SpringBoot使用Thymeleaf自定义标签代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

此篇文章内容仅限于 描述springboot与 thy 自定义标签的说明,所以你在看之前,请先会使用springboot和thymeleaf!!

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 additionalDialects=new LinkedHashSet();
  //自定义方言
  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 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 getProcessors() {
  final Set 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 getMarkupSubstitutes(Arguments arguments, Element element) {
  final Element container = new Element("div");
  final Text text = new Text("是的,这是测试");
  container.addChild(text);
 
  final List nodes = new ArrayList<>();
  nodes.add(container);
  return nodes;
 }
 
 @Override
 public int getPrecedence() {
  return 1000;
 } 
}

html使用方式:

risk:sanstitv 是我自定义用的标签



 
  
  Title
 
 
  
  
 


网站留言

热门栏目