您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Thymeleaf中怎么自定義方言,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
目前使用的所有th:x屬性都只是一個(gè)標(biāo)準(zhǔn)的、開箱即用的功能集,如果想用想要的名稱定義你自己的一組屬性(或標(biāo)簽),并在thymeleaf中使用它們來(lái)處理你的模板。你可以定義自己的方言?,F(xiàn)在我們使用自定義的方言來(lái)實(shí)現(xiàn)頁(yè)面權(quán)限過濾效果。
Thymeleaf本身提供了StandardDialect,以及結(jié)合了Spring之后提供的SpringStandardDialect。Thymeleaf默認(rèn)的語(yǔ)法 th:if等,就是定義在了StandardDialect中,th為方言的前綴,if為方言的處理器名稱。
StandardDialect的源代碼中定義了如下的內(nèi)容
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"
定義了在模板中使用時(shí),需要以 th:XX
的形式調(diào)用。
Dialect是接口,因此需要?jiǎng)?chuàng)建自定義的方言 SecurityDialect 類,然后指定具體的處理器。不直接實(shí)現(xiàn)接口,而是繼承了 AbstractProcessorDialect
抽象類,同時(shí)需要指定名稱,以及前綴 prefix。
package com.wise.tiger.dialect; /** * 自定義Thymeleaf方言:用于處理自定義方言:過濾權(quán)限操作 */ @Component public class SecurityDialect extends AbstractProcessorDialect { //方言名稱 public static final String DIALECT_NAME = "wise_authority"; //方言前綴 public static final String PREFIX = "wise"; //方言處理優(yōu)先級(jí),和標(biāo)準(zhǔn)方言平級(jí) 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容器中注冊(cè)該自定義方言,在自定義方言中需要添加方言處理器。
方言處理器有多種,都以接口的形式定義,使用元素處理器(IElementProcessor)
接口,此接口為元素Element處理的基礎(chǔ)接口。thymeleaf提供了兩種基本的IElementTagProcessor實(shí)現(xiàn),處理器可以方便地實(shí)現(xiàn)這些實(shí)現(xiàn):
org.thymeleaf.processor.element.AbstractElementTagProcessor,用于按元素名稱匹配元素事件的處理器(即不查看屬性)。
org.thymeleaf.processor.element.AbstractAttributeTagProcessor,用于按元素事件的或者屬性(也可以是元素名稱)匹配元素事件的處理器。
官方建議一般不要直接實(shí)現(xiàn)此接口實(shí)現(xiàn)我們自己的處理器,而是繼承類 AbstractAttributeTagProcessor/
AbstractElementTagProcessor。
package com.wise.tiger.dialect; //*************** import ******************/ /** * 定義方言處理器 * * <wise:authority module="department" permission="save"> * <button>添加部門</button> * </wise:authority> * * 判定當(dāng)前登錄員工所擁有的權(quán)限是否包含module及permission所定義的權(quán)限值 * 如果包含,不處理,如果不包含,隱藏該標(biāo)簽標(biāo)記的內(nèi)容 */ public class SecurityElementTagProcessor extends AbstractElementTagProcessor{ //標(biāo)簽名稱 private static final String PRO_NAME = "authority"; //優(yōu)先級(jí) private static final int PRECEDENCE = 1000; public SecurityElementTagProcessor(String dialectPrefix) { super(TemplateMode.HTML, //此處理器將僅應(yīng)用于HTML模式 dialectPrefix, //方言前綴wise,相當(dāng)于th:if中的th PRO_NAME,//處理器名稱,相當(dāng)于th:if中的if true,//應(yīng)用方言前綴作為標(biāo)簽名 null,//沒有屬性名:將按標(biāo)記名匹配 false,//屬性名不要前綴 PRECEDENCE);//方言優(yōu)先級(jí),標(biāo)準(zhǔn)方言默認(rèn)為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"); //獲取到當(dāng)前線程綁定的請(qǐng)求對(duì)象 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); //已經(jīng)拿到session,就可以拿到session中保存的用戶信息了。 Employee emp = (Employee) request.getSession().getAttribute("employee"); //構(gòu)建標(biāo)簽標(biāo)記的權(quán)限 Privilege privilege = new Privilege(module,permission); if (!isPermitted(emp,privilege)){ structureHandler.setAttribute("style","display:none"); } } /** * 判斷登錄員工是否具有操作權(quán)限 * @param emp 登錄員工 * @param privilege 權(quán)限值 * @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中怎么自定義方言了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。