项目背景: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 待测试补充。。

thymeleaf自定义标签方言处理的更多相关文章

  1. thymeleaf教程-springboot项目中实现thymeleaf自定义标签

    转载: http://www.9191boke.com/466119140.html    91博客网 开始: 在使用thymeleaf的过程中有时候需要公共部分渲染页面,这个时候使用自定义标签实现自 ...

  2. spring thymeleaf 自定义标签

    概述 thymeleaf2.1.5自定义标签及自定义属性案例,类似于JSP中的自定义JSTL标签 详细 代码下载:http://www.demodashi.com/demo/10495.html 一. ...

  3. Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  4. [JSP]自定义标签库taglib

    自定义标签的步骤 自定义标签的步骤大概有三步: 1.继承javax.servlet.jsp.tagext.*下提供的几个标签类,如Tag.TagSupport.BodyTagSupport.Simpl ...

  5. [Java] JSP笔记 - 自定义标签

    自定义标签的创建步骤: 自定义标签的四大功能: 自定义标签的类结构: 在 1.0 中呢, 可以将 <body-content> 的值设置为 JSP, 2.0中则不允许在自定义标签体中出现j ...

  6. thinkphp自定义标签库

    thinkphp ~ php中 的类, 的成员变量, 本身是没有类型说明的, 那么我怎么知道它的类型呢? 或初始值呢? 通常在类定义中, 如果能给一个初始值的(对于已知简单类型的),最好给一个初始值, ...

  7. 12 自定义标签/JSTL标签库/web国际化/java web之设计模式和案例

    EL应用      自定义一个标签,实现两个字符串的相加 1回顾      1.1servlet生命周期           init(ServletConfig)           service ...

  8. EL函数以及自定义标签的应用

    一.EL函数(调用普通类的静态方法) 编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤): ①编写一个普通的java类,提供一个静态方法,功能自定,例如下: package cn.wzbril ...

  9. JSTL 自定义标签

    编写描述标签的tld文件,把这个文件放到web-inf/目录下,才能在jsp页面上调用自定义的标签 package test.yz; import java.io.IOException; impor ...

随机推荐

  1. 【JVM从小白学成大佬】开篇

    JVM的重要性毋庸置疑,可以毫不夸张的说Java虚拟机是整个Java平台的基石. JVM方面的知识,也一直是BAT等大厂面试考核的重点.特别是JVM调优,故障排查性能调优,你知道该从哪些方面入手吗? ...

  2. html中video标签

    video标签: <video src="视频的后缀名是.webM/.ogg/.mp4 "></video>属性: src:用于告诉video标签需要播放的 ...

  3. Xshell登陆服务器及Linux的简单命令

    在之前的推文中,我已经给出了怎样利用Git登陆服务器”你在用xshell,putty登陆?推荐一个小工具(Git)登陆“其中包括xshell登陆服务器.今天讲讲常见的Linux命令,这个和之前将的利用 ...

  4. 《Java 8 in Action》Chapter 2:通过行为参数化传递代码

    你将了解行为参数化,这是Java 8非常依赖的一种软件开发模式,也是引入 Lambda表达式的主要原因.行为参数化就是可以帮助你处理频繁变更的需求的一种软件开发模式.一言以蔽之,它意味 着拿出一个代码 ...

  5. .NET导出Excel的四种方法及评测

    .NET导出Excel的四种方法及评测 导出Excel是.NET的常见需求,开源社区.市场上,都提供了不少各式各样的Excel操作相关包.本文,我将使用NPOI.EPPlus.OpenXML.Aspo ...

  6. Spring自定义属性编辑器及原理解释.md

    bean的自动装配解释 手动解决方式 自动注入解决方式 bean的自动装配解释 之前有构造注入和设值注入,但是也是手动的 autowire ="byname" 这里要注意自动装配的 ...

  7. C# HTTP系列13 以form-data方式上传多个文件以及键值对集合到远程服务器

    系列目录     [已更新最新开发文章,点击查看详细] 类似于以下场景,将表单中的用户信息(包含附件)上传到服务器并保存到数据库中, <form id="form1" run ...

  8. 实战SpringCloud响应式微服务系列教程(第四章)

    接上一篇: 实战SpringCloud响应式微服务系列教程(第一章) 实战SpringCloud响应式微服务系列教程(第二章) 实战SpringCloud响应式微服务系列教程(第三章) 1.1.4 引 ...

  9. Spring学习之旅(十三)--使用NoSQL数据库

    除了关系型数据库之外,现在还有一种 NoSQL 数据库非常流行,而 Spring 自然也没有放过对它的支持. NoSQL 数据库有很多种,如: MongoDBGenericJackson2JsonRe ...

  10. js的真值与假值

    假值 结果为 false 的值称为 假值.例如,空字符串 "" 为假值,因为在布尔表达式中,"" 等于 false. false == 0返回:true fal ...