当前位置: 移动技术网 > IT编程>开发语言>Java > Thymeleaf 3.0 自定义标签方言属性的实例讲解

Thymeleaf 3.0 自定义标签方言属性的实例讲解

2020年09月19日  | 移动技术网IT编程  | 我要评论
此篇文章内容仅限于 描述 thy3.0 自定义标签的说明,所以你在看之前,请先会使用它。直奔主题,以下代码是如何引用 第三方标签的。说明: shriodialect 是shiro 官方为thy开发的自

此篇文章内容仅限于 描述 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());
   }
   }
}

以下代码是为了向sansitiveencryptprocessor 提供的解析表达式 thymeleaf解析器,用来获取参数值的:

import org.thymeleaf.context.itemplatecontext;
import org.thymeleaf.engine.attributename;
import org.thymeleaf.exceptions.templateprocessingexception;
import org.thymeleaf.model.iprocessableelementtag;
import org.thymeleaf.standard.expression.istandardexpression;
import org.thymeleaf.standard.expression.istandardexpressionparser;
import org.thymeleaf.standard.expression.standardexpressionparser;
import org.thymeleaf.util.evaluationutils;
import org.thymeleaf.util.stringutils;
 
import java.util.arraylist;
import java.util.list;
 
import static java.util.arrays.aslist;
import static java.util.collections.unmodifiablelist;
import static org.thymeleaf.util.stringutils.trim;
import static org.thymeleaf.util.validate.notempty;
import static org.thymeleaf.util.validate.notnull;
 
public final class thymeleaffacade {
 
  private thymeleaffacade() {
    throw new unsupportedoperationexception();
  }
 
  public static string getrawvalue(final iprocessableelementtag element, final attributename attributename) {
    notnull(element, "element must not be null");
    notnull(attributename, "attributename must not be empty");
 
    final string rawvalue = trim(element.getattributevalue(attributename));
    notempty(rawvalue, "value of '" + attributename + "' must not be empty");
 
    return rawvalue;
  }
 
  public static string getrawvalue(final iprocessableelementtag element, final string attributename) {
    notnull(element, "element must not be null");
    notempty(attributename, "attributename must not be empty");
 
    final string rawvalue = trim(element.getattributevalue(attributename));
    notempty(rawvalue, "value of '" + attributename + "' must not be empty");
 
    return rawvalue;
  }
 
  public static object evaluateexpression(itemplatecontext arguments, string expression) throws templateprocessingexception {
    notnull(arguments, "arguments must not be null");
    notempty(expression, "expression must not be empty");
 
    final istandardexpressionparser parser = new standardexpressionparser();
 
    final istandardexpression evaluableexpression = parser.parseexpression(arguments, expression);
 
    return evaluableexpression.execute(arguments);
  }
 
  public static list<object> evaluateasiterable(itemplatecontext arguments, string rawvalue) throws templateprocessingexception {
    notnull(arguments, "arguments must not be null");
    notempty(rawvalue, "rawvalue must not be empty");
 
    final object evaluatedexpression = evaluateexpression(arguments, rawvalue);
 
    return evaluationutils.evaluateaslist(evaluatedexpression);
  }
 
  public static list<object> evaluateasiterableorrawvalue(itemplatecontext arguments, string rawvalue) {
    notnull(arguments, "arguments must not be null");
    notempty(rawvalue, "rawvalue must not be empty");
 
    final list<object> result = new arraylist<object>();
    try {
      result.addall(evaluateasiterable(arguments, rawvalue));
    } catch (templateprocessingexception ex) {
      result.add(rawvalue);
    }
 
    return unmodifiablelist(result);
  }
 
  public static list<string> evaluateasstringswithdelimiter(itemplatecontext arguments, string rawvalue, string delimiter) {
    notnull(arguments, "arguments must not be null");
    notempty(rawvalue, "rawvalue must not be empty");
    notempty(delimiter, "delimiter must not be empty");
 
    final list<string> result = new arraylist<string>();
    final list<object> iterates = evaluateasiterableorrawvalue(arguments, rawvalue);
 
    for (object o : iterates) {
      result.addall(aslist(stringutils.split(o, delimiter)));
    }
 
    return unmodifiablelist(result);
  }

以上为 后端代码实现内容,

页面标签使用方式:

<td risk:sansiencrypt="card:${data.payercardno}"></td>

card 是需要 加密的类型,我实现的代码里 对身份证和 手机号还有 csn 加了密。

上面的内容讲的是 标签传入数据 并返回处理数据。

类似于 if 的标签属性,就不写了。

以上这篇thymeleaf 3.0 自定义标签方言属性的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网