thymeleaf tutorial -springboot project implemented thymeleaf custom label

Reprinted:  http://www.9191boke.com/466119140.html     91 blog network

Start:

In the process of using thymeleaf sometimes need to render the public portion of the page, this time using a custom label for automatic query data for rendering more convenient, without additional rendering each page to query respectively. Links at the bottom of the site has been set up as an example, let's start the tutorial.

1.html

html as follows:

<div id="friend_link">
            友情链接:
            <div amlink:text="''" style="display: inline">
            </div>
        </div>

It can be seen that we need to define a custom label for amlink.

2. Implementing custom tag processor

ThSysTagProcessor

package webapp.customlabel;

import com.baomidou.mybatisplus.mapper.SqlRunner;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
importorg.thymeleaf.standard.expression.StandardExpressions;
 Import org.thymeleaf.templatemode.TemplateMode; 

Import java.util.List;
 Import a java.util.Map; 

/ ** 
 * custom tags 
 * / 
public  class ThSysTagProcessor the extends AbstractAttributeTagProcessor {
     Private  static  Final String TEXT_ATTRIBUTE = "text" ;
     Private  static  Final  int PRECEDENCE = 10000 ; 

    / * 
     templateMode: template mode, here using HTML templates. 
     dialectPrefix: label prefix. That xxx: text in xxx.
     elementName: matching tag element name. For example, if a div, then we custom label can only be used in a div tag. It can be null matches all tags.
     prefixElementName: label name is required prefix. 
     attributeName: Custom tag attribute names. Here is the text. 
     prefixAttributeName: prefix attribute name is required, if it is true, Thymeeleaf will be asked to use must be added the prefix text attributes, namely xxx: text. 
     precedence: priority label processing, and Thymeleaf standard dialects used herein the same priority. 
     whether to remove custom property after the labeling process: removeAttribute. 
     * / 
    Public ThSysTagProcessor (String dialectPrefix) {
         Super ( 
                TemplateMode.HTML, 
                dialectPrefix, 
                null ,
                 to false , 
                TEXT_ATTRIBUTE, 
                to true , 
                PRECEDENCE, 
                to true  );
    } 

    @Override 
    protected  void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName,
                             String attributeValue, IElementTagStructureHandler structureHandler) {
        final IEngineConfiguration configuration = context.getConfiguration();
        final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
        final IStandardExpression expression = parser.parseExpression(context, attributeValue);
        final String title = (String) expression.execute(context);

        SqlRunner sqlRunner = new SqlRunner();
        List<Map<String, Object>> mapList = sqlRunner.selectList("select * from amlink order by sort");
        StringBuilder links = new StringBuilder();
        String alink = "<a href=\"#link#\" target=\"_blank\" title=\"#a_title#\">#title#</a>";
        for (Map<String, Object> map : mapList) {
            String alinkStr = alink
                    .replaceAll("#link#", map.get("link").toString())
                    .replaceAll("#a_title#", map.get("a_title").toString())
                    .replaceAll("#title#", map.get("title").toString())
            ;
            links.append(alinkStr);
        }

        structureHandler.setBody(title + links.toString(), false);
    }
}

Note: The following is a query from the database links and spliced ​​into a string

springboot project realization thymeleaf custom label | 91 blog

 

3. The definition of dialect class ThSysDialect

 

package webapp.customlabel;

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;

public class ThSysDialect extends AbstractProcessorDialect {
    //定义方言名称
    private static final String DIALECT_NAME = "Sys Dialect";

    publicThSysDialect () {
         // set custom dialects "dialect processor" same priority 
        Super (DIALECT_NAME, "amlink" , StandardDialect.PROCESSOR_PRECEDENCE); 
    } 

    @Override 
    public the Set <IProcessor> getProcessors (String dialectPrefix) { 
        the Set <IProcessor> = Processors new new HashSet <> (); 
        processors.add ( new new ThSysTagProcessor (dialectPrefix)); 
        processors.add ( new new StandardXmlNsTagProcessor (TemplateMode.HTML, dialectPrefix));
         return Processors; 
    } 
}

4. In SpringBoot loaded custom dialects

package webapp.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import webapp.customlabel.ThSysDialect;

/**
 * Thymeleaf配置
 */
@Configuration
public class ThymeleafDialectConfig {
    @Bean
    public ThSysDialect thSysDialect() {
        return new ThSysDialect();
    }

}

 

Guess you like

Origin www.cnblogs.com/007sx/p/10963031.html