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>
未登陆时

登陆后:

thymeleaf自定义标签的更多相关文章
- spring thymeleaf 自定义标签
概述 thymeleaf2.1.5自定义标签及自定义属性案例,类似于JSP中的自定义JSTL标签 详细 代码下载:http://www.demodashi.com/demo/10495.html 一. ...
- thymeleaf自定义标签方言处理
项目背景:springboot+thymeleaf thymeleaf两种方式处理自定义标签:AbstractAttributeTagProcessor 和 AbstractElementTagPro ...
- thymeleaf教程-springboot项目中实现thymeleaf自定义标签
转载: http://www.9191boke.com/466119140.html 91博客网 开始: 在使用thymeleaf的过程中有时候需要公共部分渲染页面,这个时候使用自定义标签实现自 ...
- Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- [JSP]自定义标签库taglib
自定义标签的步骤 自定义标签的步骤大概有三步: 1.继承javax.servlet.jsp.tagext.*下提供的几个标签类,如Tag.TagSupport.BodyTagSupport.Simpl ...
- [Java] JSP笔记 - 自定义标签
自定义标签的创建步骤: 自定义标签的四大功能: 自定义标签的类结构: 在 1.0 中呢, 可以将 <body-content> 的值设置为 JSP, 2.0中则不允许在自定义标签体中出现j ...
- thinkphp自定义标签库
thinkphp ~ php中 的类, 的成员变量, 本身是没有类型说明的, 那么我怎么知道它的类型呢? 或初始值呢? 通常在类定义中, 如果能给一个初始值的(对于已知简单类型的),最好给一个初始值, ...
- 12 自定义标签/JSTL标签库/web国际化/java web之设计模式和案例
EL应用 自定义一个标签,实现两个字符串的相加 1回顾 1.1servlet生命周期 init(ServletConfig) service ...
- EL函数以及自定义标签的应用
一.EL函数(调用普通类的静态方法) 编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤): ①编写一个普通的java类,提供一个静态方法,功能自定,例如下: package cn.wzbril ...
- JSTL 自定义标签
编写描述标签的tld文件,把这个文件放到web-inf/目录下,才能在jsp页面上调用自定义的标签 package test.yz; import java.io.IOException; impor ...
随机推荐
- paramiko免密登陆
paramiko免密登陆 # -*- coding: utf-8 -*- import paramiko pkey='D:/pycharm_workspace/testpy/ssh_paramiko_ ...
- Python并发编程——paramiko远程控制的模块、病毒攻击原理、dll注入、
文章目录 paramiko模块 作业 攻击原理解析 一.什么是dll 二.为何要有dll 什么是dll注入: 什么时候需要dll注入 dll注入的方法 使用SetWindowsHookEx函数对应用程 ...
- DDMS查看Threads情况
有时候程序运行出现死锁或者信号量卡死是很纠结的问题,单看代码很难分析定位问题,这时候可以借助DDMS来查看threads的运行情况,一目了然. 手机连接上USB,确保adb连通,然后启动Eclipse ...
- 《流畅的Python》 读书笔记 第二章数据结构(2) 231011
2.5 对序列使用+和* 通常 + 号两侧的序列由相同类型的数据所构成,在拼接的过程中,两个被操作的序列都不会被修改,Python 会新建一个包含同样类型数据的序列来作为拼接的结果 +和*都遵循这个规 ...
- Chromium 消息循环和线程池详解
Chromium 中的多线程机制由 base 库提供,要理解 Chromium 中的多线程机制,首先要理解的概念就是 base::MessageLoop 和 base::TaskScheduler , ...
- YbtOJ 质数与约数 4.统计元素
搜不到题解,乐. 由题意,\(a_i\) 对答案有贡献当且仅当 \(i\in{[l,r]}\) 且对于 \(\forall a_j| {a_i},j\notin[l,r]\). 则对于每个 \(a_i ...
- postgresql 去重计数改写案例
最近帮忙在搞一个内网报表系统的项目,里面的逻辑比较复杂,很多视图套视图的语句. 最多的一个视图除了它本身以外,一层层嵌套了7个视图在里面,贼恶心. SQL遇到性能问题只能每一层视图捋清对象关系来排查缓 ...
- easy_enc
寻找main函数,步过好像没有main函数,这时候想要F5就只能找到对的函数了 F5以后发现进行的操作有很多,这里发现有几个都没用的 后面发现主要是对V5有操作,所以直接看有关于V5的操作函数 经过上 ...
- Ubuntu18虚拟机远程开发
Ubuntu18 虚拟机远程开发 1. 安装 VMware 和 Ubuntu18 虚拟机 (1)VMware 官网上下载免费版本 一路 next 安装就行(中间也许需要改一下存放路径) (2)Ubun ...
- 2022.7.15 jiazhaopeng 讲课纪要
前言 由于难度仍然过高,难度再次下调,这节课主要在水绿.这节课讲的是并查集,之所以是以绿题为主是因为上了绿之后的题有一大半都要结合别的东西,初中生不会,所以只能讲的简单一点. ORZ%%% jzp学长 ...