springboot + Thymeleaf自定义标签

32次阅读

共计 4095 个字符,预计需要花费 11 分钟才能阅读完成。

前言
最近一直在忙着写自己的博客系统,好久没有发布新的文章,最近在查 Thymeleaf 的自定义标签发现文章极少,非常苦恼,简书的这篇解决了我的问题,于是引用一下他的文章(抄一下)
心路历程
我的博客的标题我需要显示出当前在哪个页面,如 ” 后台首页 -XX 的博客 ”,” 文章管理 -XX 的博客 ”,如果说我把对象每个页面都传递过去那肯定是没有问题的,但是我并不想,于是就开始找解决办法,最开始用拦截器解决,实现倒是可以实现,就是 ajax 方法会报错,虽然可以用,但是不够优雅;由于开始一直在写博客后台,没考虑首页的问题,当我放弃使用拦截器以后,我把我的 map 放到了 session 中,这样登录以后肯定也是所有页面共享。再后来博客后台写的差不多了,准备开始写前台首页的时候发现这个方法也不对,总之这次自己写项目觉得不考虑清楚就写最后就会各种不对劲,就一直在改。最终还是决定用 Thymeleaf 的自定义标签来解决这个问题。
代码
html 标签
<title thSys:text=”‘ 后台登录 – ‘”></title>
实现自定义标签处理器
新建一个 java 类 com.songhaozhi.mayday.config.thymeleaf.tag.ThSysTagProcessor.java
对于 Thymeleaf 方言,自定义标签的处理逻辑是在标签处理器定义的。自定义标签处理器需要实现 AbstractAttributeTagProcessor 接口,标签的处理逻辑在 doProcess 方法中编写。
package com.songhaozhi.mayday.config.thymeleaf.tag;

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 com.songhaozhi.mayday.model.dto.MaydayConst;

/**
* @author 作者: 宋浩志
* @createDate 创建时间:2018 年 12 月 17 日 上午 10:44:54
*/
public class ThSysTagProcessor extends AbstractAttributeTagProcessor{
private static final String TEXT_ATTRIBUTE = “text”;
private static final int PRECEDENCE = 10000;
/*templateMode: 模板模式,这里使用 HTML 模板。
dialectPrefix: 标签前缀。即 xxx:text 中的 xxx。在此例子中 prefix 为 thSys。
elementName:匹配标签元素名。举例来说如果是 div,则我们的自定义标签只能用在 div 标签中。为 null 能够匹配所有的标签。
prefixElementName: 标签名是否要求前缀。
attributeName: 自定义标签属性名。这里为 text。
prefixAttributeName:属性名是否要求前缀,如果为 true,Thymeeleaf 会要求使用 text 属性时必须加上前缀,即 thSys: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);
structureHandler.setBody(title+MaydayConst.options.get(“blog_name”),false);
}

}

其中的 MaydayConst.options.get(“blog_name”) 是我数据库查出来缓存的 map
定义方言类
package com.songhaozhi.mayday.config.thymeleaf.dialect;

import java.util.HashSet;
import java.util.Set;

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 com.songhaozhi.mayday.config.thymeleaf.tag.ThSysTagProcessor;

/**
* 系统方言
* @author 宋浩志
* @createDate 创建时间:2018 年 12 月 4 日 下午 9:12:14
*
*/
public class ThSysDialect extends AbstractProcessorDialect{
// 定义方言名称
private static final String DIALECT_NAME=”Sys Dialect”;

public ThSysDialect() {
// 设置自定义方言与 ” 方言处理器 ” 优先级相同
super(DIALECT_NAME, “thSys”, StandardDialect.PROCESSOR_PRECEDENCE);
}

@Override
public Set<IProcessor> getProcessors(String dialectPrefix) {
Set<IProcessor> processors=new HashSet<IProcessor>();
processors.add(new ThSysTagProcessor(dialectPrefix));
processors.add(new StandardXmlNsTagProcessor(TemplateMode.HTML, dialectPrefix));
return processors;
}

}

在 SpringBoot 中加载自定义方言
com.songhaozhi.mayday.config.ThymeleafDialectConfig.java
package com.songhaozhi.mayday.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.songhaozhi.mayday.config.thymeleaf.dialect.ThSysDialect;

/**
* Thymeleaf 配置
* @author 宋浩志
* @createDate 创建时间:2018 年 12 月 4 日 下午 9:29:53
*
*/
@Configuration
public class ThymeleafDialectConfig {
@Bean
public ThSysDialect thSysDialect() {
return new ThSysDialect();
}
}

效果
其中 ” 后台登录 – “ 是标签传递到后台的,” 张三的博客 ” 是数据库保存的值。这就大功告成了!代码在我的博客项目里面 Mayday 博客系统(还没有写完)如果对你有帮助你给我一个 star,非常感谢!!!

正文完
 0