thymeleaf教程-springboot項目中實現thymeleaf自定義標籤

轉載: http://www.9191boke.com/466119140.html    91博客網html

開始:java

在使用thymeleaf的過程當中有時候須要公共部分渲染頁面,這個時候使用自定義標籤實現自動查詢數據進行渲染比較方便,不用額外在每一個頁面渲染中去分別查詢。已網站底部的友情連接設置爲例子,下面開始教程。web

1.htmlspring

html以下:sql

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

能夠看出咱們須要定義的自定義標籤爲amlink。數據庫

2.實現自定義標籤處理器express

ThSysTagProcessorspringboot

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;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.List;
import java.util.Map;

/**
 * 自定義標籤
 */
public class ThSysTagProcessor extends AbstractAttributeTagProcessor {
    private static final String TEXT_ATTRIBUTE  = "text";
    private static final int PRECEDENCE = 10000;

    /*
     templateMode: 模板模式,這裏使用HTML模板。
     dialectPrefix: 標籤前綴。即xxx:text中的xxx。
     elementName:匹配標籤元素名。舉例來講若是是div,則咱們的自定義標籤只能用在div標籤中。爲null可以匹配全部的標籤。
     prefixElementName: 標籤名是否要求前綴。
     attributeName: 自定義標籤屬性名。這裏爲text。
     prefixAttributeName:屬性名是否要求前綴,若是爲true,Thymeeleaf會要求使用text屬性時必須加上前綴,即xxx:text。
     precedence:標籤處理的優先級,此處使用和Thymeleaf標準方言相同的優先級。
     removeAttribute:標籤處理後是否移除自定義屬性。
     */
    public ThSysTagProcessor(String dialectPrefix) {
        super(
                TemplateMode.HTML,
                dialectPrefix,
                null,
                false,
                TEXT_ATTRIBUTE,
                true,
                PRECEDENCE,
                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);
    }
}

注:如下爲從數據庫中查詢出友情連接並拼接成一個字符串mybatis

springboot項目中實現thymeleaf自定義標籤 | 91博客

 

3.定義方言類ThSysDialectapp

 

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";

    public ThSysDialect() {
        //設置自定義方言與"方言處理器"優先級相同
        super(DIALECT_NAME, "amlink", StandardDialect.PROCESSOR_PRECEDENCE);
    }

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

4.在SpringBoot中加載自定義方言

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();
    }

}
相關文章
相關標籤/搜索