惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
月光博客
月光博客
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
U
Unit 42
腾讯CDC
爱范儿
爱范儿
J
Java Code Geeks
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
B
Blog
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
thymeleaf(3)自定义标签
祈雨的笔记 · 2018-02-23 · via 祈雨的笔记

1、申明自定义标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package wxtx.com.thymeleaf;
import java.util.HashSet;
import java.util.Set;
import org.springframework.stereotype.Component;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.spring4.dialect.SpringStandardDialect;

@Component
public class TXDialect extends SpringStandardDialect {
public String getPrefix() {
return "wxtx";
}

@Override
public Set<IProcessor> getProcessors() {
final Set<IProcessor> processors = new HashSet<IProcessor>();
processors.add(new TXElementProcessor());
return processors;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Node;
import org.thymeleaf.dom.Text;
import org.thymeleaf.processor.element.AbstractMarkupSubstitutionElementProcessor;

@Component
public class TXElementProcessor extends AbstractMarkupSubstitutionElementProcessor {
public TXElementProcessor() {
super("hello");
}

@Override
protected List<Node> getMarkupSubstitutes(Arguments arguments, Element element) {

String name = element.getAttributeValue("name");
List<Node> nodes = null;

if("none".equals(name)){

Element h2 = new Element("strong");
h2.setAttribute("style", "color:red");
h2.addChild(new Text("无信息"));
element.insertChild(0, h2);
nodes = element.getChildren();
} else {

Element container = new Element("div");
String content = "hello "+name;
Text text = new Text(content);
container.addChild(text);
nodes = new ArrayList<>();
nodes.add(container);
}

return nodes;
}

@Override
public int getPrecedence() {
return 1000;
}
}

2、注册自定义标签

2.1、spring4

修改spring配置文件

1
2
3
4
5
6
7
8
9
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<property name="dialects">
<set>
<!-- 添加自定义标签 -->
<bean class="wxtx.com.thymeleaf.TXDialect" />
</set>
</property>
</bean>

2.2、springboot

springboot默认使用thymeleaf引擎,所以不需要在配置文件中注册自定义标签,仅需要将自定义标签类添加入spring容器即可。例如添加@Component注解。

3、使用方法

1
2
<wxtx:hello name="小吴">123<strong>456</strong></wxtx:hello>
<wxtx:hello name="none">123<strong>456</strong></wxtx:hello>

4、效果

thymeleaf渲染后的html源码

1
2
<div>hello 小吴</div>
<strong style="color:red">无信息</strong>123<strong>456</strong>

5、获取表达式变量

https://www.thymeleaf.org/doc/articles/sayhelloagainextendingthymeleafevenmore5minutes.html