原创

springboot+thymeleaf 实现自定义标签

今天试着封装了thymeleaf的自定义标签,实现的效果是这样
比如

<my cte="span" name="profileName" ></my>

输出的标签是这样

<span>这是测试</span>

首先定义自己的标签处理类
MyTagProcessor.java

package com.common;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IAttribute;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.spring5.context.SpringContextUtils;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.Map;

/**
 * @Author: haohaowang
 * @Desc
 * @Date: 2019/3/26 19:30
 */
//AbstractElementTagProcessor要继承模板提供的抽象类
public class MyTagProcessor extends AbstractElementTagProcessor {

    private static final String TAG_NAME = "my";
    private static final int PRECEDENCE = 300;


    public MyTagProcessor(TemplateMode templateMode, String dialectPrefix, String elementName, boolean prefixElementName, String attributeName, boolean prefixAttributeName, int precedence) {
        super(templateMode, dialectPrefix, elementName, prefixElementName, attributeName, prefixAttributeName, precedence);
    }

    @Override//这里处理标签的逻辑
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag,
                             IElementTagStructureHandler handler) {
                             //通过下面代码可以查询数据库
        //ApplicationContext app = SpringContextUtils.getApplicationContext(context);
     //  Xxx mapper=app.getBean(Xxx.class);

         IAttribute cte = tag.getAttribute("cte");//获取标签中cte的属性如果在自定义标签中没有没有此属性就会返回 null
        String cteValue =  cte.getValue();//获取属性值
        String  tagValue = "<"+cte+">这是测试</"+cte+">"
        handler.replaceWith(tagValue, false);
    }


     /*templateMode: 模板模式,这里使用HTML模板。
     dialectPrefix: 标签前缀。即xxx:text中的xxx。在此例子中prefix为thSys。
     elementName:匹配标签元素名。举例来说如果是div,则我们的自定义标签只能用在div标签中。为null能够匹配所有的标签。
     prefixElementName: 标签名是否要求前缀。
     attributeName: 自定义标签属性名。这里为text。
     prefixAttributeName:属性名是否要求前缀,如果为true,Thymeeleaf会要求使用text属性时必须加上前缀,即thSys:text。
     precedence:标签处理的优先级,此处使用和Thymeleaf标准方言相同的优先级。
     removeAttribute:标签处理后是否移除自定义属性。*/


    public MyTagProcessor(String dialectPrefix) {
        super(TemplateMode.HTML,
                dialectPrefix,
                TAG_NAME,
                true,
                null,
                false,
                PRECEDENCE);
    }

}

Dialect.java

package com.conf;

import com.common.MyTagProcessor;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;
import org.thymeleaf.standard.processor.StandardXmlNsTagProcessor;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Author: haohaowang
 * @Desc
 * @Date: 2019/3/26 19:31
 */
 //定义方言类
public class Dialect extends AbstractProcessorDialect {

    private final static String NAME = "my";


    public Dialect() {
        //设置自定义方言与"方言处理器"优先级相同
        super(NAME, null, StandardDialect.PROCESSOR_PRECEDENCE);
    }

    @Override
    public Set<IProcessor> getProcessors(String dialectPrefix) {
        Set<IProcessor> processors = new HashSet<IProcessor>();
        processors.add(new MyTagProcessor(dialectPrefix));
        processors.add(new StandardXmlNsTagProcessor(TemplateMode.HTML, dialectPrefix));

        return processors;
    }

    private Set<IProcessor> createStandardProcessorsSet(String dialectPrefix) {
        LinkedHashSet<IProcessor> processors = new LinkedHashSet<IProcessor>();
      //    添加自定义标签处理器,可添加多个
        processors.add(new MyTagProcessor(dialectPrefix));
        return processors;
    }
}

添加到Spring容器中
DialectConfig.java

package com.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: haohaowang
 * @Desc
 * @Date: 2019/3/26 19:32
 */
@Configuration
public class DialectConfig {
    @Bean
    public Dialect getDialect() {
        return new Dialect();
    }
}

下面是我自己封装的效果
file
file

正文到此结束(点击广告是对作者最大的支持)