• thymeleaf自定义标签方言处理


     项目背景:springboot+thymeleaf

     thymeleaf两种方式处理自定义标签:AbstractAttributeTagProcessor 和 AbstractElementTagProcessor

    一、AbstractAttributeTagProcessor :

    1. 定义dialog

    package com.spt.im.web.config;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.thymeleaf.dialect.AbstractProcessorDialect;
    import org.thymeleaf.processor.IProcessor;
    
    public class CustomDialect extends AbstractProcessorDialect{
        
        private static final String DIALECT_NAME = "staticFile";
        private static final String PREFIX = "W";
        public static final int PROCESSOR_PRECEDENCE = 1000;
        @Value("${im.static.resources}")
        private String filePath;
        
    
        protected CustomDialect() {
            super(DIALECT_NAME, PREFIX, PROCESSOR_PRECEDENCE);
        }
    
        @Override
        public Set<IProcessor> getProcessors(String dialectPrefix) {
            final Set<IProcessor> processors = new HashSet<IProcessor>();
            processors.add(new SampleJsTagProcessor(dialectPrefix, filePath));
            processors.add(new SampleCssTagProcessor(dialectPrefix, filePath));
            processors.add(new SampleSrcTagProcessor(dialectPrefix, filePath));
            return processors;
        }
    
    }

    2. 定义处理器

    package com.spt.im.web.config;
    
    import org.thymeleaf.IEngineConfiguration;
    import org.thymeleaf.context.ITemplateContext;
    import org.thymeleaf.engine.AttributeName;
    import org.thymeleaf.model.IProcessableElementTag;
    import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
    import org.thymeleaf.processor.element.IElementTagStructureHandler;
    import org.thymeleaf.standard.expression.IStandardExpression;
    import org.thymeleaf.standard.expression.IStandardExpressionParser;
    import org.thymeleaf.standard.expression.StandardExpressions;
    import org.thymeleaf.templatemode.TemplateMode;
    
    public class SampleJsTagProcessor extends AbstractAttributeTagProcessor{
        
         private static final String ATTR_NAME = "js";
         private static final String ELE_NAME = "script";
         private static final int PRECEDENCE = 10000;
         private String filePath;
    
        protected SampleJsTagProcessor(String dialectPrefix, String filePath) {
             super(
                    TemplateMode.HTML, 
                    dialectPrefix,     
                    ELE_NAME,             
                    false,             
                    ATTR_NAME,         
                    true,              
                    PRECEDENCE,       
                    true); 
             this.filePath = filePath;
        }
    
        @Override
        protected void doProcess(ITemplateContext context,
                IProcessableElementTag tag, AttributeName attributeName,
                String attributeValue, IElementTagStructureHandler structureHandler) {
            final IEngineConfiguration configuration = context.getConfiguration();
            final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
            final IStandardExpression expression = parser.parseExpression(context, attributeValue);
            final String url = (String) expression.execute(context);
            structureHandler.setAttribute("src", filePath + url);
        }
    
    }

    3. 添加到配置中

    package com.spt.im.web.config;
    
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class WorkImport {
    
        @Bean
        public CustomDialect testDialect(){
            return new CustomDialect();
        }
    }

    4. 页面中使用

    <script W:js="@{/static/js/pinyin.js}" type="text/javascript"></script>

    OK,完毕,项目运行,上述标签会被替换成相应的链接。

    二、AbstractElementTagProcessor 待测试补充。。

  • 相关阅读:
    http请求类型简介
    关于PLSQL连接报错:ORA-12154:TNS:无法解析指定的连接标识符
    JDK8的安装与配置
    今天新装tomcat遇到黑窗口(startup.bat)启动乱码问题解决!!!
    一个简单的工厂模式(一个接口,多个实现,通过调用条件的不同,分别去调用符合的实现)
    数组(复习)
    java选择结构、循环结构(复习)
    java常用的数据类型,变量和常量,运算符(复习)
    java输入输出,书写规范,运行原理,跨平台原理(复习)
    JAVA基础入门(JDK、eclipse下载安装)
  • 原文地址:https://www.cnblogs.com/eric-fang/p/8376575.html
Copyright © 2020-2023  润新知