此篇文章內容僅限于 描述 thy3.0 自定義標簽的說明,所以你在看之前,請先會使用它。
直奔主題,以下代碼是如何參考 第三方標簽的。說明: shrioDialect 是Shiro 官方為thy開發的自定義標簽工具。和jsp的一樣
RiskDialect 是我寫的自定義標簽
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<property name="additionalDialects">
<set>
<!-- thymeleaf 使用shiro標簽 -->
<bean class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>
<bean class="com.hpay.risk.boss.common.RiskDialect"/>
</set>
</property>
</bean>
首先看代碼:
import java.util.LinkedHashSet;
import java.util.Set;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;
/**
*@author Garc
*@Date 2017年2月16日 上午11:42:51
*@info thymeleaf 自定義標簽屬性
*@snise
**/
public class RiskDialect extends AbstractProcessorDialect {
private static final String NAME = "Risk";
private static final String PREFIX = "risk";
public RiskDialect() {
super(NAME, PREFIX, StandardDialect.PROCESSOR_PRECEDENCE);
}
@Override
public Set<IProcessor> getProcessors(String dialectPrefix) {
return createStandardProcessorsSet(dialectPrefix);
}
private Set<IProcessor> createStandardProcessorsSet(String dialectPrefix) {
LinkedHashSet<IProcessor> processors = new LinkedHashSet<IProcessor>();
processors.add(new SansitiveEncryptProcessor(dialectPrefix));
return processors;
}
}
我定義了 RiskDialect 類,并需要繼承 thymeleaf 官方 方言類
我定義的這個是為了做敏感資料加密用的。 這是前段代碼。
以下是實作自定義標簽方言 代碼:
import static at.pollux.thymeleaf.shiro.processor.ThymeleafFacade.evaluateAsStringsWithDelimiter;
import static at.pollux.thymeleaf.shiro.processor.ThymeleafFacade.getRawValue;
import java.util.List;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;
import org.unbescape.html.HtmlEscape;
import com.hpay.utils.StringUtils;
/**
*@author Garc
*@Date 2017年2月16日 上午11:48:34
*@info 敏感加密標簽
*@snise
**/
public class SansitiveEncryptProcessor extends AbstractAttributeTagProcessor{
private static final String DELIMITER = ",";
private static final String ATTRIBUTE_NAME = "sansiEncrypt";
private static final int PRECEDENCE = 300;
private static final String CARD="card";
private static final String MOBILE="mobile";
private static final String IDENTITY="identity";
private static final String CSN="csn";
protected SansitiveEncryptProcessor( String dialectPrefix) {
super(
TemplateMode.HTML, // 處理thymeleaf 的模型
dialectPrefix, // 標簽前綴名
null, // No tag name: match any tag name
false, // No prefix to be applied to tag name
ATTRIBUTE_NAME, // 標簽前綴的 屬性 例如:< risk:sansiEncrypt="">
true, // Apply dialect prefix to attribute name
PRECEDENCE, // Precedence (inside dialect's precedence)
true); // Remove the matched attribute afterwards
}
@Override
protected void doProcess(ITemplateContext context,
IProcessableElementTag tag, AttributeName attributeName,
String attributeValue, IElementTagStructureHandler structureHandler) {
final String rawValue = getRawValue(tag, attributeName); //獲取標簽內容運算式
String type=null;
String exper=null;
if(StringUtils.isNotBlank(rawValue)){
type=rawValue.split(":")[0]; //獲取型別
exper=rawValue.split(":")[1]; //獲取運算式
}
//通過IStandardExpression 決議器 決議運算式獲取引數
final List<String> values = evaluateAsStringsWithDelimiter(context, exper, DELIMITER);
final String elementCompleteName = tag.getElementCompleteName(); //標簽名
//創建模型
final IModelFactory modelFactory = context.getModelFactory();
final IModel model = modelFactory.createModel();
//添加模型 標簽
model.add(modelFactory.createOpenElementTag(elementCompleteName));
for (String value : values) {
//創建 html5標簽 文本回傳資料
if(CARD.equals(type)){
model.add(modelFactory.createText(HtmlEscape.escapeHtml5(getCardNo(value))));
}else if(MOBILE.equals(type)){
model.add(modelFactory.createText(HtmlEscape.escapeHtml5(getMobile(value))));
}else if(IDENTITY.equals(type)){
model.add(modelFactory.createText(HtmlEscape.escapeHtml5(getIdentity(value))));
}else if(CSN.equals(type)){
model.add(modelFactory.createText(HtmlEscape.escapeHtml5(getCsn(value))));
}
}
//添加模型 標簽
model.add(modelFactory.createCloseElementTag(elementCompleteName));
//替換頁面標簽
structureHandler.replaceWith(model, false);
}
protected String getCardNo(String cardNo) {
if (StringUtils.isNotBlank(cardNo) && cardNo.length() >= 9) {
return cardNo.substring(0, 4) + cardNo.substring(4, cardNo.length() - 3).replaceAll("[0-9]", "*") + cardNo.substring(cardNo.length() - 4, cardNo.length());
}
return cardNo;
}
protected static String getIdentity(String val){
if(org.apache.commons.lang.StringUtils.isBlank(val)||val.length()<9){
return val;
}else{
return val.substring(0, 4)+ val.substring(4, val.length()-4).replaceAll("[0-9]", "*") + val.substring(val.length()-4, val.length());
}
}
/**
* 前四后四顯示
* @param val
* @return
*/
protected static String getMobile(String val){
if(org.apache.commons.lang.StringUtils.isBlank(val)||val.length()<9){
return val;
}else{
return val.substring(0, 3)+ val.substring(4, val.length()-3).replaceAll("[0-9]", "*") + val.substring(val.length()-4, val.length());
}
}
/**
* 星星顯示
* @param val
* @return
*/
protected String getCsn(String val){
if(org.apache.commons.lang.StringUtils.isBlank(val)||val.length()<12){
return val;
}else{
return val.substring(0, 2)+ val.substring(2, val.length()-3).replaceAll("[0-9a-zA-Z]", "*") + val.substring(val.length()-6, val.length());
}
}
}
以上為 后端代碼實作內容,
頁面標簽使用方式:
<td risk:sansiEncrypt="card:${data.payerCardNo}"></td> card 是需要 加密的型別,我實作的代碼里 對身份證和 手機號還有 CSN 加了密。
上面的內容講的是 標簽傳入資料 并回傳處理資料。
類似于 if 的標簽屬性,就不寫了,由于在上班,就寫這么多,如果想了解更多,可以加QQ群 119170668:親自找我,我為你指導。群主就是我!
uj5u.com熱心網友回復:
網上找了很久,全都是SpringBoot配方言的,終于找到了傳統SSM框架構建,Bean裝配的方式,之前快要成功配到Dialect屬性里面了,結果是在additionalDialects額外的方言里面,佛了。。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130945.html
標籤:Web 開發
上一篇:Java安裝
下一篇:xxl-rpc remoting request fail, http HttpStatus[302] invalid.
