这期内容当中小编将会给大家带来有关Thymeleaf中怎么自定义方言,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
目前使用的所有th:x属性都只是一个标准的、开箱即用的功能集,如果想用想要的名称定义你自己的一组属性(或标签),并在thymeleaf中使用它们来处理你的模板。你可以定义自己的方言。现在我们使用自定义的方言来实现页面权限过滤效果。
Thymeleaf本身提供了StandardDialect,以及结合了Spring之后提供的SpringStandardDialect。Thymeleaf默认的语法 th:if等,就是定义在了StandardDialect中,th为方言的前缀,if为方言的处理器名称。
StandardDialect的源代码中定义了如下的内容
public class StandardDialect
extends AbstractProcessorDialect
implements IExecutionAttributeDialect, IExpressionObjectDialect {
public static final String NAME = "Standard";
public static final String PREFIX = "th";
public static final int PROCESSOR_PRECEDENCE = 1000;
...
}
其中的 PREFIX = "th"
定义了在模板中使用时,需要以 th:XX
的形式调用。
Dialect是接口,因此需要创建自定义的方言 SecurityDialect 类,然后指定具体的处理器。不直接实现接口,而是继承了 AbstractProcessorDialect
抽象类,同时需要指定名称,以及前缀 prefix。
package com.wise.tiger.dialect;
/**
* 自定义Thymeleaf方言:用于处理自定义方言:过滤权限操作
*/
@Component
public class SecurityDialect extends AbstractProcessorDialect {
//方言名称
public static final String DIALECT_NAME = "wise_authority";
//方言前缀
public static final String PREFIX = "wise";
//方言处理优先级,和标准方言平级
public static final int PROCESSOR_PRECEDENCE = 1000;
public SecurityDialect() {
super(DIALECT_NAME, PREFIX, PROCESSOR_PRECEDENCE);
}
//添加方言处理器
@Override
public Set<IProcessor> getProcessors(String dialectPrefix) {
final Set<IProcessor> processors = new HashSet<>();
processors.add(new SecurityElementTagProcessor(dialectPrefix));
return processors;
}
}
@Component表示向Spring IoC容器中注册该自定义方言,在自定义方言中需要添加方言处理器。
方言处理器有多种,都以接口的形式定义,使用元素处理器(IElementProcessor)
接口,此接口为元素Element处理的基础接口。thymeleaf提供了两种基本的IElementTagProcessor实现,处理器可以方便地实现这些实现:
org.thymeleaf.processor.element.AbstractElementTagProcessor,用于按元素名称匹配元素事件的处理器(即不查看属性)。
org.thymeleaf.processor.element.AbstractAttributeTagProcessor,用于按元素事件的或者属性(也可以是元素名称)匹配元素事件的处理器。
官方建议一般不要直接实现此接口实现我们自己的处理器,而是继承类 AbstractAttributeTagProcessor/
AbstractElementTagProcessor。
package com.wise.tiger.dialect;
//*************** import ******************/
/**
* 定义方言处理器
*
* <wise:authority module="department" permission="save">
* <button>添加部门</button>
* </wise:authority>
*
* 判定当前登录员工所拥有的权限是否包含module及permission所定义的权限值
* 如果包含,不处理,如果不包含,隐藏该标签标记的内容
*/
public class SecurityElementTagProcessor extends AbstractElementTagProcessor{
//标签名称
private static final String PRO_NAME = "authority";
//优先级
private static final int PRECEDENCE = 1000;
public SecurityElementTagProcessor(String dialectPrefix) {
super(TemplateMode.HTML, //此处理器将仅应用于HTML模式
dialectPrefix, //方言前缀wise,相当于th:if中的th
PRO_NAME,//处理器名称,相当于th:if中的if
true,//应用方言前缀作为标签名
null,//没有属性名:将按标记名匹配
false,//属性名不要前缀
PRECEDENCE);//方言优先级,标准方言默认为1000
}
@Override
protected void doProcess(ITemplateContext context,
IProcessableElementTag tag,
IElementTagStructureHandler structureHandler) {
//获取tag的module属性值
String module = tag.getAttributeValue("module");
//获取tag的permission属性值
String permission = tag.getAttributeValue("permission");
//获取到当前线程绑定的请求对象
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
//已经拿到session,就可以拿到session中保存的用户信息了。
Employee emp = (Employee) request.getSession().getAttribute("employee");
//构建标签标记的权限
Privilege privilege = new Privilege(module,permission);
if (!isPermitted(emp,privilege)){
structureHandler.setAttribute("style","display:none");
}
}
/**
* 判断登录员工是否具有操作权限
* @param emp 登录员工
* @param privilege 权限值
* @return
*/
private boolean isPermitted(Employee emp, Privilege privilege){
for(Role role : emp.getRoles()){
if(role.getPrivileges().contains(privilege)){
return true;
}
}
return false;
}
}
<html xmlns:th="http://www.thymeleaf.org" xmlns:wise="http://www.thymeleaf.org">
。。。。。。。。
<wise:authority module="department" permission="save">
<button>添加部门</button>
</wise:authority>
上述就是小编为大家分享的Thymeleaf中怎么自定义方言了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4134962/blog/3072942