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 ,如果想要格式 ...
随机推荐
- package.json详解以及package-lock.json的作用
一.创建 package.json输入如下命令之后,会要求填写基本的配置信息,这里,我们选择一路回车即可,待生成 package.json 文件之后,再来配置. npm init 二.配置 packa ...
- [错误解决] Libreoffice转换不成功,直接不做任何操作
问题描述: Libreoffice在版本5.3.0之前都存在这个问题.现象是:当你运行其中一个LibreOffice的时候,再运行另外一个Libreoffice转换时,将不做任何操作. 解决方案: 如 ...
- java html实体转义
public static void main(String[] args) { String str = " -<->-&-"-'" ...
- EasyNVR摄像机网页无插件直播方案H5前端构建之:bootstrap-datepicker日历插件的实时动态展现
EasyNVR场景需求 基础:不管是城市监控还是园区管理或者是幼儿园监控,这些安防监控需求已经成为我们生活中不可或缺的重要一环,这不仅仅是提升城市管理水平和人民群众安全感的现实需求,也是完善社会治安消 ...
- 快速读取TXT几百万行数据, 然后插入到数据,SqlBulkCopy功能的确是有效率
public static void Main(string[] args) { string strPath = "F:\\Download\\600.txt"; int lin ...
- laravel操作mongo详细说明
原文地址:http://returnc.com/detail/3728 一个Eloquent模型和Query构建器,支持MongoDB,使用原始的Laravel API.该库扩展了原始的Larav ...
- Python 的语言特性
谈谈对 Python 和其他语言的区别 Python 是一门语法简洁优美,功能强大无比,应用领域非常广泛,具有强大完备的第三方库,他是一门强类型的可移植.可扩展,可嵌入的解释型编程语言,属于动态语言. ...
- Google Adsense(谷歌网站联盟)广告申请指南
Google AdSense 是一种获取收入的快速简便的方法,适合于各种规模的网站发布商.它可以在网站的内容网页上展示相关性较高的 Google 广告,并且这些广告不会过分夸张醒目.由于所展示的广告同 ...
- vmware 虚拟机中有时获取不到IP地址
转载: https://blog.csdn.net/valecalida/article/details/80683518 解决方法:打开vmware,然后找到编辑 然后点击虚拟网络编辑器 此时应该先 ...
- jmeter jtl 文件
一.获取.jtl文件 使用非 GUI 模式,即命令行模式运行 JMeter .执行完成jmeter后,会生成jtl文件. 1.1. 命令介绍 1)先cmd进入到jmeter的bin文件目录下(这里是 ...