SpringBoot使用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 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
    
    
      
      
    

以上内容就是全部教程,下班了。不编了!如果想了解更多,可以加QQ群 119170668:亲自找我,我为你指导。群主就是我!

 

你可能感兴趣的:(java)