博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
thymeleaf自定义标签方言处理
阅读量:4676 次
发布时间:2019-06-09

本文共 3557 字,大约阅读时间需要 11 分钟。

 项目背景: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
getProcessors(String dialectPrefix) { final Set
processors = new HashSet
(); 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;@Configurationpublic class WorkImport {    @Bean    public CustomDialect testDialect(){        return new CustomDialect();    }}

4. 页面中使用

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

二、AbstractElementTagProcessor 待测试补充。。

转载于:https://www.cnblogs.com/eric-fang/p/8376575.html

你可能感兴趣的文章
xmapp中 使用admin的权限打开mysql时出现错误1045
查看>>
Objective-C--Runtime机制
查看>>
古文选读161篇--蔡礼旭老师选
查看>>
jquery easyui grid 表格特殊字符处理
查看>>
Android学习之ViewPager
查看>>
Spring笔记
查看>>
LeetCode Weekly Contest 126
查看>>
8封装的意义和拓展性
查看>>
leetcode15
查看>>
c# Path.Combine
查看>>
noip 2015 子串
查看>>
Windows 窗体控件中的多线程处理之:如何对 Windows 窗体控件进行线程安全调用...
查看>>
C#逐行读取文本文件
查看>>
PHp 密码验证
查看>>
sql 的join
查看>>
世界虽大,但没有破不了的wifi
查看>>
计算机网络中的TCP/IP协议与OSI模型
查看>>
[Javascript] Log Levels and Semantic Methods
查看>>
oo第四次作业
查看>>
UVa 10346 - Peter's Smokes
查看>>