此篇文章內(nèi)容僅限于 描述springboot與 thy 自定義標(biāo)簽的說(shuō)明,所以你在看之前,請(qǐng)先會(huì)使用springboot和thymeleaf!!
之前寫過(guò)一篇是springMVC與thymeleaf 的自定義標(biāo)簽(屬于自定義方言的屬性一塊,類似thy的th:if和th:text等)文章,如果你想了解,以下是地址:
點(diǎn)擊>>Thymeleaf3.0自定義標(biāo)簽屬性
這篇例子可以實(shí)現(xiàn)你的分頁(yè)標(biāo)簽實(shí)現(xiàn)等功能,不會(huì)講一堆的廢話和底層的原理(自行百度),屬于快速上手教程,請(qǐng)認(rèn)真看以下內(nèi)容!
PS: 請(qǐng)?jiān)试S我將thymeleaf簡(jiǎn)稱thy,springboot簡(jiǎn)稱sb
依然直奔主題,sb本身是自帶thy的,而且使用方式也很簡(jiǎn)單,直接配置application.properties 這個(gè)文件就可以了,當(dāng)然你不配也是可以的。但是,需要配置自定義方言的話,就需要自己把配置重新寫出來(lái),看下面代碼:
說(shuō)明:RiskDialect是我自己的自定義標(biāo)簽,而且從這個(gè)配置可以簡(jiǎn)單看出,spring視圖的配置通過(guò)注解的方式將thymeleaf配置進(jìn)去了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@Configuration public class TemplateEngineConfig{ @Bean public ContentNegotiatingViewResolver getViewResolver(){ ServletContextTemplateResolver templateResolver= new ServletContextTemplateResolver(); templateResolver.setPrefix( "/WEB-INF/views/" ); templateResolver.setSuffix( ".html" ); templateResolver.setTemplateMode( "HTML5" ); templateResolver.setCacheable( false ); templateResolver.setCharacterEncoding( "UTF-8" ); Set<IDialect> additionalDialects= new LinkedHashSet<IDialect>(); //自定義方言 additionalDialects.add( new RiskDialect()); SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setAdditionalDialects(additionalDialects); templateEngine.setTemplateResolver(templateResolver); ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver(); thymeleafViewResolver.setTemplateEngine(templateEngine); thymeleafViewResolver.setCharacterEncoding( "UTF-8" ); thymeleafViewResolver.setOrder( 1 ); List<ViewResolver> viewResolvers= new ArrayList<>(); viewResolvers.add(thymeleafViewResolver); ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver(); viewResolver.setViewResolvers(viewResolvers); return viewResolver; } } |
接下來(lái)看RiskDialect實(shí)現(xiàn):
說(shuō)明:SanstitvEncryptProcessor這個(gè)類是 thymeleaf處理器,用來(lái)處理定義方言邏輯的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.garc.thymeleaf.dialect; import org.springframework.stereotype.Component; import org.thymeleaf.dialect.AbstractDialect; import org.thymeleaf.dialect.AbstractXHTMLEnabledDialect; import org.thymeleaf.processor.IProcessor; import java.util.HashSet; import java.util.Set; /** * Created by Garc on 2018/1/17. */ public class RiskDialect extends AbstractDialect { private static final String PREFIX= "risk" ; private static final String ELEMENT_NAME= "sanstitv" ; @Override public String getPrefix() { return PREFIX; } @Override public Set<IProcessor> getProcessors() { final Set<IProcessor> processors = new HashSet<>(); processors.add( new SanstitvEncryptProcessor(ELEMENT_NAME)); return processors; } } |
繼續(xù)看SanstitvEncryptProcessor這個(gè)類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package com.garc.thymeleaf.dialect; import org.springframework.context.ApplicationContext; import org.thymeleaf.Arguments; import org.thymeleaf.dom.Element; import org.thymeleaf.dom.Node; import org.thymeleaf.dom.Text; import org.thymeleaf.processor.ProcessorResult; import org.thymeleaf.processor.element.AbstractElementProcessor; import org.thymeleaf.processor.element.AbstractMarkupSubstitutionElementProcessor; import org.thymeleaf.spring4.context.SpringWebContext; import java.util.ArrayList; import java.util.List; /** * Created by Garc on 2018/1/17. */ public class SanstitvEncryptProcessor extends AbstractMarkupSubstitutionElementProcessor { protected SanstitvEncryptProcessor(String elementName) { super (elementName); } @Override protected List<Node> getMarkupSubstitutes(Arguments arguments, Element element) { final Element container = new Element( "div" ); final Text text = new Text( "是的,這是測(cè)試" ); container.addChild(text); final List<Node> nodes = new ArrayList<>(); nodes.add(container); return nodes; } @Override public int getPrecedence() { return 1000 ; } } |
html使用方式:
risk:sanstitv 是我自定義用的標(biāo)簽
1
2
3
4
5
6
7
8
9
10
11
12
|
<!DOCTYPE html> < html lang = "en" xmlns = "http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org" xmlns:risk = "http://www.w3.org/1999/xhtml" > < head > < meta content = "text/html;charset=UTF-8" ></ meta > < title >Title</ title > </ head > < body > < span th:text = "${test}" ></ span > < risk:sanstitv path = "測(cè)試" ></ risk:sanstitv > </ body > </ html > |
以上這篇SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/Qensq/article/details/79109531