thymeleaf教程-springboot项目中实现thymeleaf自定义标签
转载: http://www.9191boke.com/466119140.html 91博客网
开始:
在使用thymeleaf的过程中有时候需要公共部分渲染页面,这个时候使用自定义标签实现自动查询数据进行渲染比较方便,不用额外在每个页面渲染中去分别查询。已网站底部的友情链接设置为例子,下面开始教程。
1.html
html如下:
<div id="friend_link">
友情链接:
<div amlink:text="''" style="display: inline">
</div>
</div>
可以看出我们需要定义的自定义标签为amlink。
2.实现自定义标签处理器
ThSysTagProcessor
package webapp.customlabel; import com.baomidou.mybatisplus.mapper.SqlRunner;
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; import java.util.List;
import java.util.Map; /**
* 自定义标签
*/
public class ThSysTagProcessor extends AbstractAttributeTagProcessor {
private static final String TEXT_ATTRIBUTE = "text";
private static final int PRECEDENCE = 10000; /*
templateMode: 模板模式,这里使用HTML模板。
dialectPrefix: 标签前缀。即xxx:text中的xxx。
elementName:匹配标签元素名。举例来说如果是div,则我们的自定义标签只能用在div标签中。为null能够匹配所有的标签。
prefixElementName: 标签名是否要求前缀。
attributeName: 自定义标签属性名。这里为text。
prefixAttributeName:属性名是否要求前缀,如果为true,Thymeeleaf会要求使用text属性时必须加上前缀,即xxx:text。
precedence:标签处理的优先级,此处使用和Thymeleaf标准方言相同的优先级。
removeAttribute:标签处理后是否移除自定义属性。
*/
public ThSysTagProcessor(String dialectPrefix) {
super(
TemplateMode.HTML,
dialectPrefix,
null,
false,
TEXT_ATTRIBUTE,
true,
PRECEDENCE,
true);
} @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 title = (String) expression.execute(context); SqlRunner sqlRunner = new SqlRunner();
List<Map<String, Object>> mapList = sqlRunner.selectList("select * from amlink order by sort");
StringBuilder links = new StringBuilder();
String alink = "<a href=\"#link#\" target=\"_blank\" title=\"#a_title#\">#title#</a>";
for (Map<String, Object> map : mapList) {
String alinkStr = alink
.replaceAll("#link#", map.get("link").toString())
.replaceAll("#a_title#", map.get("a_title").toString())
.replaceAll("#title#", map.get("title").toString())
;
links.append(alinkStr);
} structureHandler.setBody(title + links.toString(), false);
}
}
注:以下为从数据库中查询出友情链接并拼接成一个字符串
3.定义方言类ThSysDialect
package webapp.customlabel; 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.Set; public class ThSysDialect extends AbstractProcessorDialect {
//定义方言名称
private static final String DIALECT_NAME = "Sys Dialect"; public ThSysDialect() {
//设置自定义方言与"方言处理器"优先级相同
super(DIALECT_NAME, "amlink", StandardDialect.PROCESSOR_PRECEDENCE);
} @Override
public Set<IProcessor> getProcessors(String dialectPrefix) {
Set<IProcessor> processors = new HashSet<>();
processors.add(new ThSysTagProcessor(dialectPrefix));
processors.add(new StandardXmlNsTagProcessor(TemplateMode.HTML, dialectPrefix));
return processors;
}
}
4.在SpringBoot中加载自定义方言
package webapp.conf; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import webapp.customlabel.ThSysDialect; /**
* Thymeleaf配置
*/
@Configuration
public class ThymeleafDialectConfig {
@Bean
public ThSysDialect thSysDialect() {
return new ThSysDialect();
} }
thymeleaf教程-springboot项目中实现thymeleaf自定义标签的更多相关文章
- springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据
springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...
- Springboot项目中 前端展示本地图片
Springboot项目中 前端展示本地图片 本文使用的是Springboot官方推荐的thymeleaf(一种页面模板技术) 首先在pom文件加依赖 <dependency> <g ...
- SpringBoot项目中,表单的验证操作
在创建Springboot项目中,我们使用了表单验证操作,这一操作将极大地简化我们编程的开发 1.接收数据,以及验证 @PostMapping("/save") public Mo ...
- springboot项目中接口入参的简单校验
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- 国际化的实现i18n--错误码国际化以及在springboot项目中使用
国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母); 主要涉及3个类: Locale用来设置定制的语言和国家代码 Resource ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
- 自身使用的springboot项目中比较全的pom.xml
在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...
- springboot 项目中获取默认注入的序列化对象 ObjectMapper
在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...
随机推荐
- CKA认证简介
- spark + hive
1.如何让 spark-sql 能够访问hive? 只需将hive-site.xml 放到 spark/conf 下即可,hive-site.xml 内容请参照hive集群搭建 2.要在spark 代 ...
- [转帖]两大容器管理平台,Kubernetes与OpenShift有什么区别?
两大容器管理平台,Kubernetes与OpenShift有什么区别? https://www.sohu.com/a/327413642_100159565 原来openshift 就是 k8s的一个 ...
- 56 容器(十)——Iterator迭代器遍历容器
迭代器的获取 LIst与Set容器统一使用他们的对象.Iterator()方法获得迭代器对象,然后使用while循环配合迭代器的方法hasNext()及next()来遍历容器. List<Str ...
- nodeJs编写的简单服务器
一.简单的nodeJs写的 http 服务器 1.先Hello world,创建最简单的 Node 服务器(server.js) var http = require("http" ...
- 笔记:npm常见错误
常见错误 破坏的npm安装 随机错误 找不到兼容版本 权限错误 Error: ENOENT, stat 'C:\Users\<user>\AppData\Roaming\npm' 在Win ...
- Drools入门
文章转载自:http://cwqcwq.iteye.com/blog/397869 一.背景知识: 1.什么是规则引擎 Java规则引擎起源于基于规则的专家系统,而基于规则的专家系统又是专家系统的 ...
- 【Maven基础入门】01 Maven的安装与环境变量的配置
写在前面: Mavne,作为一个优秀的项目构建工具,虽说我们平时在使用的时候或多或少的会使用到它,但了解仅限于它能构建项目,然后其他的就不知道了. 以及仓库.POM父类文件.等等. 工欲善其事,必先利 ...
- C#泛型集合之——列表
列表基础 1.列表概述:列表与哈希集合不同之处在于,它的元素可以重复.(更接近逻辑上的数组,而哈希集合更接近于数学上的集合) 2.创建及初始化: (1)List<类型> 列表名 =new ...
- mysql 5.7 非正常安装,无法启动 服务没有报告任何错误
以前,完整安装mysql5.7程序时,由于程序太大,可以将安装缓存目录中的安装文件(较小)复制出来后,留以后使用. mysql--win32.msi 2 mysql-5.7.17-winx64.msi ...