thymeleaf自定义标签
前言
使用thymeleaf自定义标签,环境:springboot 2.3.7 + thymeleaf 3.0.11(2021-01-14最新版)
由于使用shiro,我们需要与thymeleaf整合一些标签方便更好操作,以isAuthenticated
为例。
ps:虽然有一个开源的框架,但是很久没维护了,还不如照着官网来自定义。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
参考官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/extendingthymeleaf.html
官方demo:https://github.com/thymeleaf/thymeleafexamples-extrathyme
一、创建方言
前缀仍然使用th
package com.example.shirodemo.config.thymeleaf;
import java.util.HashSet;
import java.util.Set;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;
/**
* @author 绫小路
* @date 2021/1/14 22:43
* @description
*/
public class ShiroDialect extends AbstractProcessorDialect {
private static final String DIALECT_NAME = "shiro方言";
public ShiroDialect() {
// We will set this dialect the same "dialect processor" precedence as
// the Standard Dialect, so that processor executions can interleave.
super(DIALECT_NAME, "th", StandardDialect.PROCESSOR_PRECEDENCE);
}
@Override
public Set<IProcessor> getProcessors(String s) {
final Set<IProcessor> processors = new HashSet<>();
processors.add(new AuthTagProcessor(s));
return processors;
}
}
二、标签处理
package com.example.shirodemo.config.thymeleaf;
import static org.thymeleaf.standard.processor.StandardWithTagProcessor.PRECEDENCE;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
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.templatemode.TemplateMode;
/**
* @author 绫小路
* @date 2021/1/14 23:02
* @description
*/
public class AuthTagProcessor extends AbstractAttributeTagProcessor {
private static final String SHIRO = "shiro";
public AuthTagProcessor(String dialectPrefix) {
super(
TemplateMode.HTML, // This processor will apply only to HTML mode
dialectPrefix, // Prefix to be applied to name for matching 前缀
null, // No tag name: match any tag name
false, // No prefix to be applied to tag name
SHIRO, // Name of the attribute that will be matched
true, // Apply dialect prefix to attribute name
PRECEDENCE, // Precedence (inside dialect's own precedence)优先级
true); // Remove the matched attribute afterwards
// super( TemplateMode.HTML, dialectPrefix, elementName, prefixElementName, isAuthenticated, prefixAttributeName, precedence, removeAttribute);
}
@Override
protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue,
IElementTagStructureHandler structureHandler) {
Subject subject = SecurityUtils.getSubject();
if ("isAuthenticated".equals(attributeValue)) {//判断是否登录
if (!subject.isAuthenticated()) {
structureHandler.removeElement();//未登录时将元素移除 例如<a href="/admin" th:shiro="isAuthenticated">admin</a>
}
}
}
}
三、配置
直接交给spring容器管理即可
@Bean
public ShiroDialect shiroDialect() {
return new ShiroDialect();//添加自定义的标签
}
四、使用
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
首页 <a href="/login">登录</a>
<br>
<a href="/logout" th:shiro="isAuthenticated">注销</a>
<br>
<a href="/admin" th:shiro="isAuthenticated">admin</a>
</body>
</html>
未登陆时
登陆后: