General label is nothing more than two, the first is that we export to label content (similar to th: each). The second element is based on whether the label inside the label (similar to th: if).
1. Based on the springboot1.5 + thymeleaf2.1
1. The first label control content is displayed label
This is divided into two, one is used as the label, one is as a property.
as follows:
<mytag:displayele name="1"> Here to see </mytag:displayele> <p mytag:displayattr="false"> Here we see 123456 </p>
step:
1. establish their own dialect
package cn.qlq.myTag; amount java.util.Collections; amount java.util.HashSet; amount java.util.Set; import org.thymeleaf.dialect.AbstractDialect; import org.thymeleaf.processor.IProcessor; public class MyAutoDialect extends AbstractDialect { private static final String PREFIX = "mytag"; private static final Set<IProcessor> processors = new HashSet<IProcessor>(); static { processors.add(new HasPermissionAttrProcessor()); processors.add(new HasPermissionElementProcessor()); } public String getPrefix() { return PREFIX; } @Override public Set<IProcessor> getProcessors() { return Collections.unmodifiableSet(processors); } }
2. To establish its own processor
(1) attribute processor
package cn.qlq.myTag; import org.thymeleaf.Arguments; import org.thymeleaf.dom.Attribute; import org.thymeleaf.dom.Element; import org.thymeleaf.processor.attr.AbstractConditionalVisibilityAttrProcessor; /** * As an attribute determination * * @author Administrator * */ public class HasPermissionAttrProcessor extends AbstractConditionalVisibilityAttrProcessor { private static final String ATTRIBUTE_NAME = "displayattr"; private static final int PRECEDENCE = 300; protected HasPermissionAttrProcessor() { super(ATTRIBUTE_NAME); } @Override public int getPrecedence() { return PRECEDENCE; } @Override public Boolean The isVisible ( Final the Arguments arguments, Final the Element Element, Final String attributeName) { // is judged (name is displayed when 1 or true) == course, we can use other attributes can also be used in accordance with the attribute name attribute name element for other operations the Attribute attributeFromNormalizedName = element.getAttributeFromNormalizedName (attributeName); IF (! attributeFromNormalizedName = null ) { String escapedValue = attributeFromNormalizedName.getEscapedValue(); if ("1".equals(escapedValue) || "true".equals(escapedValue)) { return true; } } return false; } }
(2) processor elements (tab element is preferably not capitalized)
package cn.qlq.myTag; import org.thymeleaf.Arguments; import org.thymeleaf.dom.Attribute; import org.thymeleaf.dom.Element; import org.thymeleaf.processor.element.AbstractConditionalVisibilityElementProcessor; /** * Element of judgment * * @author Administrator * */ public class HasPermissionElementProcessor extends AbstractConditionalVisibilityElementProcessor { private static final String ELEMENT_NAME = "displayele"; private static final int PRECEDENCE = 300; protected HasPermissionElementProcessor() { super(ELEMENT_NAME); } // determining element is visible @Override public Boolean The isVisible ( Final the Arguments arguments, Final the Element Element) { // is judged (name is displayed when 1 or true) == The element name attribute course, we can use other properties, but also can be done by other operations name attribute the attribute attributeFromNormalizedName = element.getAttributeFromNormalizedName ( "name" ); IF (attributeFromNormalizedName =! null ) { String escapedValue = attributeFromNormalizedName.getEscapedValue(); if ("1".equals(escapedValue) || "true".equals(escapedValue)) { return true; } } return false; } @Override public int getPrecedence() { return PRECEDENCE; } @Override public boolean removeHostElementIfVisible(final Arguments arguments, final Element element) { return true; } }
3. Registration own dialect
package cn.qlq.myTag; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyAutoDialectConfiguration { @Bean public MyAutoDialect myAutoDialect() { return new MyAutoDialect(); } }
4. The interface is as follows:
<!doctype html> <html lang="zh_CN" xmlns:th="http://www.thymeleaf.org" xmlns:mytag="http://thymeleafexamples"> <head> <meta charset="UTF-8"/> <title>后台登录-X-admin2.0</title> </head> <body class="login-bg"> <mytag:displayele name="false"> See here </mytag:displayele> <mytag:displayele name="1"> Here to see </mytag:displayele> <p mytag:displayattr="false"> Here we see 123456 </p> <p mytag:displayattr="1"> Look here to 123456 </p> </body> </html>
result:
2. Customize the output label content label
This is divided into two, one is used as the label, one is as a property.
1. Custom Processor
(1) The processor element
package cn.qlq.myTag; import java.util.ArrayList; import java.util.List; import org.thymeleaf.Arguments; import org.thymeleaf.dom.Attribute; import org.thymeleaf.dom.Element; import org.thymeleaf.dom.Node; import org.thymeleaf.dom.Text; import org.thymeleaf.processor.element.AbstractMarkupSubstitutionElementProcessor; /** * As elements * * @author Administrator * */ public class SetContentElementProcessor extends AbstractMarkupSubstitutionElementProcessor { private static final String ELEMENT_NAME = "setcontentele"; private static final int PRECEDENCE = 300; protected SetContentElementProcessor() { super(ELEMENT_NAME); } /** * Equivalent to return information node */ @Override protected List<Node> getMarkupSubstitutes(Arguments arguments, Element element) { final Element container = new Element("div"); TextStr String = "data from the background" ; // 读取元素的name属性 Attribute attributeFromNormalizedName = element.getAttributeFromNormalizedName("name"); if (attributeFromNormalizedName != null) { String escapedValue = attributeFromNormalizedName.getEscapedValue(); textStr += "name = " + escapedValue; } final Text text = new Text(textStr); container.addChild(text); final List<Node> nodes = new ArrayList<>(); nodes.add(container); return nodes; } @Override public int getPrecedence() { return PRECEDENCE; } }
(2) attribute processor
package cn.qlq.myTag; import java.util.List; import org.thymeleaf.Arguments; import org.thymeleaf.dom.Attribute; import org.thymeleaf.dom.Element; import org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor; /** * As an attribute determination * * @author Administrator * */ public class SetContentAttrProcessor extends AbstractTextChildModifierAttrProcessor { private static final String ATTRIBUTE_NAME = "setcontentattr"; private static final int PRECEDENCE = 300; protected SetContentAttrProcessor() { super(ATTRIBUTE_NAME); } @Override public int getPrecedence() { return PRECEDENCE; } @Override protected String getText(Arguments arguments, Element element, String attributeName) { // 读取元素的name属性 Attribute attributeFromNormalizedName = element.getAttributeFromNormalizedName(attributeName); if (attributeFromNormalizedName != null) { String escapedValue = attributeFromNormalizedName.getEscapedValue(); return "attributeName: " + attributeName + ", value: " + escapedValue; } return ""; } }
Join in 2.dialect
processors.add(new SetContentElementProcessor()); processors.add(new SetContentAttrProcessor());
3. Use Interface
<mytag:setcontentele name="3">
Custom label
</mytag:setcontentele>
<div mytag:setcontentattr="3">
<p>取nameAttr</p>
</div>
result: