Home > Back-end >  Thymeleaf 3.0 custom tag dialect properties, simple instance. Learn quickly!
Thymeleaf 3.0 custom tag dialect properties, simple instance. Learn quickly!

Time:10-19

Content of this article is limited to describe thy3.0 custom tag, so what are you looking at before, please use it,

To the point, the following code is how to reference a third party labels, note: shrioDialect is Shiro official custom tags for thy development tools, and JSP as
RiskDialect was written by my custom tag
 & lt; Bean id="templateEngine" & gt; 





First look at the code:
 import Java. Util. LinkedHashSet; 
import java.util.Set;

The import org. Thymeleaf. The dialect. AbstractProcessorDialect;
The import org. Thymeleaf. Processor. IProcessor;
Import org, thymeleaf standard. StandardDialect;

/* *
* @ author Garc
* @ Date on February 16, 2017 in the morning 11:42:51
* @ info thymeleaf custom tag attribute
* @ snise
* */
Public class RiskDialect extends AbstractProcessorDialect {

Private static final String NAME="Risk".
Private static final String PREFIX="risk";

Public RiskDialect () {
Super (NAME, and PREFIX, StandardDialect PROCESSOR_PRECEDENCE);
}

@ Override
Public Set GetProcessors (String dialectPrefix) {
Return createStandardProcessorsSet (dialectPrefix);
}

Private Set CreateStandardProcessorsSet (String dialectPrefix) {
LinkedHashSet Processors=new LinkedHashSet (a);
Processors. The add (new SansitiveEncryptProcessor (dialectPrefix));
Return processors;
}

}

I defined the RiskDialect class, and we need to inherit thymeleaf official dialect class
I defined the sensitive data encryption is to do, this is the first code,
Below is to realize the custom tag dialect code:
 import static ats. Pollux. Thymeleaf. Shiro. Processor. ThymeleafFacade. EvaluateAsStringsWithDelimiter; 
The import static ats. Pollux. Thymeleaf. Shiro. Processor. ThymeleafFacade. GetRawValue;

Import the Java. Util. List;

The import org. Thymeleaf. Context. ITemplateContext;
The import org. Thymeleaf. Engine. AttributeName;
The import org. Thymeleaf. Model. IModel;
The import org. Thymeleaf. Model. IModelFactory;
The import org. Thymeleaf. Model. IProcessableElementTag;
The import org. Thymeleaf. Processor. Element. AbstractAttributeTagProcessor;
The import org. Thymeleaf. Processor. Element. IElementTagStructureHandler;
The import org. Thymeleaf. Templatemode. Templatemode;
The import org. Unbescape. HTML. HtmlEscape;

The import com. Hpay. Utils. StringUtils;

/* *
* @ author Garc
* @ Date on February 16, 2017 in the morning 11:48:34
* @ info sensitive encrypted tag
* @ snise
* */
Public class SansitiveEncryptProcessor extends AbstractAttributeTagProcessor {

Private static final String DELIMITER=", ";
Private static final String ATTRIBUTE_NAME="sansiEncrypt";
Private static final ints 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,//handle thymeleaf model
DialectPrefix,//tag prefix name
Null,//No tag name: match any tag name
False,//No prefix to be applied to the tag name
ATTRIBUTE_NAME,//tag prefix properties such as: & lt; Risk: sansiEncrypt="" & gt;
True,//the Apply the dialect prefix to attribute name
PRECEDENCE,//PRECEDENCE (inside the 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=https://bbs.csdn.net/topics/getRawValue (tag, attributeName);//the label content expression
String type=null;
String exper=null;
{if (StringUtils. IsNotBlank (rawValue))
Type=rawValue. The split [0] (" : ");//get type
Exper=rawValue. Split (" : ") [1];//get expression
}
//by IStandardExpression parser analytical expressions for parameter
Final List Values=evaluateAsStringsWithDelimiter (context, exper, DELIMITER);
Final String elementCompleteName=tag. GetElementCompleteName ();//tag name
//create the model
Final IModelFactory modelFactory=context. GetModelFactory ();
Final IModel model=modelFactory. CreateModel ();
//add the model tag
Model. The add (modelFactory. CreateOpenElementTag (elementCompleteName));
For (String value: values) {
//create HTML tag text data returned
If (CARD) equals (type)) {
Model. The add (modelFactory createText (HtmlEscape escapeHtml5 (getCardNo (value))));
} else if (MOBILE) equals (type)) {
Model. The add (modelFactory createText (HtmlEscape escapeHtml5 (getMobile (value))));
} else if (IDENTITY equals (type)) {
Model. The add (modelFactory createText (HtmlEscape escapeHtml5 (getIdentity (value))));
} else if (CSN) equals (type)) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related