這期內容當中小編將會給大家帶來有關Thymeleaf中怎么自定義方言,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
目前使用的所有th:x屬性都只是一個標準的、開箱即用的功能集,如果想用想要的名稱定義你自己的一組屬性(或標簽),并在thymeleaf中使用它們來處理你的模板。你可以定義自己的方言?,F在我們使用自定義的方言來實現頁面權限過濾效果。
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中怎么自定義方言了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。