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 ...
随机推荐
- 6.swagger完善:界面显示注释+多版本控制
周末,写点简单的水一下. 新版本的vs创建项目的时候可以选择自带一个swagger.然而这只是基本的swagger功能. 几个接口无所谓啦,随着接口越来越多,就这么丢给你,一时间也会懵逼,所以这篇文章 ...
- WEB项目开发流程介绍
web开发流程 web开发流程图 一.需求分析阶段 在需求分析阶段,即上图所述 "需求明确"之前的阶段 产品经理PM召集需要项目相关人员,开需求讨论会.讲解原型 相关人员需要以此了 ...
- Sentinel源码改造,实现Nacos双向通信!
Sentinel Dashboard(控制台)默认情况下,只能将配置规则保存到内存中,这样就会导致 Sentinel Dashboard 重启后配置规则丢失的情况,因此我们需要将规则保存到某种数据源中 ...
- 手撕Vue-数据驱动界面改变下
经过上一篇的介绍,数据驱动界面改变 v-model 的双向绑定已告一段落, 剩余的就以这篇文章来完成. 首先完成我们的 v-html,v-text, 其实很简单,就是将我们之前的 v-model 创建 ...
- CSS必学:你需要知道的盒子模型的秘密
作者:WangMin 格言:努力做好自己喜欢的每一件事 作为前端开发来说,要掌握的CSS基础一定很多,那么CSS中盒子模型肯定是必考必问必掌握的前端知识点,因为它是CSS基础中非常重要的内容,接下来我 ...
- Linux中修改文件权限方法
在此记录一下,看到的一篇写的比较好的帮我解决问题的文章 这里也补充一下,如果要修改当前目录下的所有文件时,文件名用 * 代替,如:chmod 755 *(即可修改当前文件下所有文件的权限为:-rwxr ...
- 题解 CF916C
题目大意: 要求构造一张图,并让该图满足以下条件: 有 \(n\) 个点,\(m\) 条边. 每条边的边权范围是 \([1,10^9]\). 图中从 \(1\) 到 \(n\) 的最短路径长度是个质数 ...
- JavaScript高级程序设计笔记10 函数Function
函数 1.几种实例化函数对象的方式 以函数声明的方式定义 函数表达式 箭头函数(arrow function) 使用Function构造函数 接收任意多个字符串参数,最后一个参数始终会被当成函数体,而 ...
- GPTs Hunter 是什么?
原文: https://openaigptguide.com/openai-gpts-hunter/ GPTs Hunter 是一个功能强大的免费导航网站,支持多语言,提供用户友好的界面. GPTs ...
- L2-036 网红点打卡攻略
#include <bits/stdc++.h> using namespace std; const int N = 210; int w[N][N]; int main() { int ...