springboot+thymeleaf3.0自定義方言
阿新 • • 發佈:2019-01-02
java版飛機大戰程式碼
為啥不用jsp自定義標籤
原文地址
jsp自定義標籤很強大,但是隻適用於jsp頁面,在html中就不行了。
準備,首先需要把springboot1.5.4預設的thymeleaf升級為3.0,不然使用不了
也就是springboot1.5.4,thymeleaf3.0
在posm.xml檔案中插入
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId >
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect -->
<dependency>
<groupId >nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.0.0</version>
</dependency>
這裡升級很危險,必須所有依賴jar包全部都給下載了
前面準備好就開始寫程式碼,我直接帖程式碼
TagDialect
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;
import org.thymeleaf.standard.processor.StandardXmlNsTagProcessor;
import org.thymeleaf.templatemode.TemplateMode;
import java.util.HashSet;
import java.util.Set;
/**
* @ClassName CustomLabel
* @Description 按鈕許可權自定義標籤名:tags
* @Author asus
* @Date Created by asus on 2018/11/2112:02
* @Version 1.0
**/
public class TagDialect extends AbstractProcessorDialect{
public TagDialect() {
super("Tags Dialect", "tags", 1000);
}
/**
*@Author asus
*@Descript元素處理器:“matter”標籤。
*@Date 16:05 2018/11/21
*@Param [s]
*@return java.util.Set<org.thymeleaf.processor.IProcessor>
**/
@Override
public Set<IProcessor> getProcessors(final String dialectPrefix) {
final Set<IProcessor> processors = new HashSet<>();
processors.add(new DictMyTagProcessor(dialectPrefix));//新增我們定義的標籤
processors.add(new StandardXmlNsTagProcessor(TemplateMode.HTML, dialectPrefix));
return processors;
}
}
DictMyTagProcessor
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.spring4.context.SpringContextUtils;
import org.thymeleaf.templatemode.TemplateMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @ClassName DictMyTagProcessor
* @Description 按鈕許可權自定義標籤:功能類
* @Author asus
* @Date Created by asus on 2018/11/2111:53
* @Version 1.0
**/
public class DictMyTagProcessor extends AbstractElementTagProcessor{
private static final String TAG_NAME = "tag";//標籤名 tag 這個玩意就是 自定義標籤的 : tag, 應該是可以定義多個標籤
private static final int PRECEDENCE = 1000;//優先順序
public DictMyTagProcessor(final String dialectPrefix) {
super(
TemplateMode.HTML, // 此處理器將僅應用於HTML模式
dialectPrefix,// 要應用於名稱的匹配字首
TAG_NAME, // 標籤名稱:匹配此名稱的特定標籤
true,// 沒有應用於標籤名的字首
null,// 無屬性名稱:將通過標籤名稱匹配
false, // 沒有要應用於屬性名稱的字首
PRECEDENCE// 優先(內部方言自己的優先
);
}
/**
*@Author asus
*@Descript context 頁面上下文 tag 標籤
*@Date 10:27 2018/11/23
*@Param [context, tag, structureHandler]
*@return void
**/
@Override
protected void doProcess(
final ITemplateContext context,
final IProcessableElementTag tag,
final IElementTagStructureHandler structureHandler) {
//獲取應用程式上下文。
ApplicationContext appCtx = SpringContextUtils.getApplicationContext(context);
ZnlyOperatorService znlyOperatorService=appCtx.getBean(ZnlyOperatorService.class);
ZnlyUserService znlyUserService=appCtx.getBean(ZnlyUserService.class);
/**
*@Author asus
*@Descript 從標籤讀取“menuId”“operatorType”“tabIndex”屬性。標籤中的這個可選屬性將告訴我們需要什麼樣的資料
*@Date 16:24 2018/11/21
*@Param [context, tag, structureHandler]
*@return void
**/
final String menuId= tag.getAttributeValue("menuId");//選單id
final String operatorType= tag.getAttributeValue("operatorType");//按鈕型別
final String tabIndex= tag.getAttributeValue("tabIndex");//標籤頁
String roleIds = "";//角色id
// 1.建立輸出的html的流
StringBuffer sb = new StringBuffer();
//此處開始替換要輸出的內容
BaseController b=new BaseController();
ZnlyUser znlyUser=b.getUser();
List<ZnlyRole> znlyRoleList=znlyUserService.findZnlyUserRoleList(znlyUser.getId());//獲取當前使用者角色id
//編譯角色id
for (int i = 0; i < znlyRoleList.size(); i++) {
if (i == 0) {
roleIds += znlyRoleList.get(i).getRoleId();
}else {
roleIds += ","+znlyRoleList.get(i).getRoleId();
}
}
List<Map<String, Object>> operList =new ArrayList<>();
try {
//獲取當前使用者的操作
operList = znlyOperatorService.getOptionByPageName(roleIds,menuId,operatorType,tabIndex);
}catch (Exception e){
e.printStackTrace();
}
String operatorButtonType="submit";
if(operatorType.equals("1")){
for (Map<String, Object> oper : operList) {//<button type="submit" class="btn btn-primary">查詢</button>
Integer buttonType=Integer.parseInt(oper.get("operator_button_type").toString());
if(1!=buttonType){
operatorButtonType="button";
}
sb.append("<button type='"+operatorButtonType+"' class='btn "+oper.get("operator_class")+"'>"+oper.get("operator_name")+"</button>");
}
}else if(operatorType.equals("2")){
for (Map<String, Object> oper : operList) {//<button type="submit" class="btn btn-primary">查詢</button>
Integer buttonType=Integer.parseInt(oper.get("operator_button_type").toString());
if(1!=buttonType){
operatorButtonType="button";
}
sb.append("<button type='"+operatorButtonType+"' class='btn btn-xs "+oper.get("operator_class")+"'>"+oper.get("operator_name")+"</button>");
}
}
//此處結束替換要輸出的內容
/**
*@Author asus
*@Descript 建立將替換自定義標籤的DOM結構。logo將顯示在“<div>”標籤內,因此必須首先建立,然後必須向其中新增一個節點。
*@Date 16:25 2018/11/21
*@Param [context, tag, structureHandler]
*@return void
**/
final IModelFactory modelFactory = context.getModelFactory();
final IModel model = modelFactory.createModel();
model.add(modelFactory.createText(sb));
/**
*@Author asus
*@Descript指示引擎用指定的模型替換整個元素。
*@Date 16:33 2018/11/21
*@Param [context, tag, structureHandler]
*@return void
**/
structureHandler.replaceWith(model, false);
}
}
最後在Application.java中新增
@Bean
public TagDialect tagDialect(){
return new TagDialect();
}
頁面引用
在頂部的html中
這裡大家可以看到我是用來shiro的控制,但是沒自由找到可以動態顯示的,就想起jsp 的自定義標籤發現thymeleaf3也有方言就是這樣,希望對你有幫助