此篇文章內容僅限于 描述springboot與 thy 自定義標簽的說明,所以你在看之前,請先會使用springboot和thymeleaf!!
之前寫過一篇是springMVC與thymeleaf 的自定義標簽(屬于自定義方言的屬性一塊,類似thy的th:if和th:text等)文章,如果你想了解,以下是地址:
這篇例子可以實現你的分頁標簽實現等功能,不會講一堆的廢話和底層的原理(自行百度),屬于快速上手教程,請認真看以下內容!
PS: 請允許我將thymeleaf簡稱thy,springboot簡稱sb
依然直奔主題,sb本身是自帶thy的,而且使用方式也很簡單,直接配置application.properties 這個文件就可以了,當然你不配也是可以的。但是,需要配置自定義方言的話,就需要自己把配置重新寫出來,看下面代碼:
說明:RiskDialect是我自己的自定義標簽,而且從這個配置可以簡單看出,spring視圖的配置通過注解的方式將thymeleaf配置進去了
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; } } |
接下來看RiskDialect實現:
說明:SanstitvEncryptProcessor這個類是 thymeleaf處理器,用來處理定義方言邏輯的
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; } } |
繼續看SanstitvEncryptProcessor這個類:
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( "是的,這是測試" ); container.addChild(text); final List<Node> nodes = new ArrayList<>(); nodes.add(container); return nodes; } @Override public int getPrecedence() { return 1000 ; } } |
html使用方式:
risk:sanstitv 是我自定義用的標簽
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 = "測試" ></ risk:sanstitv > </ body > </ html > |
以上這篇SpringBoot使用Thymeleaf自定義標簽的實例代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/Qensq/article/details/79109531