這篇“怎么用Thymeleaf創(chuàng)建Spring Boot項目”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么用Thymeleaf創(chuàng)建Spring Boot項目”文章吧。
成都創(chuàng)新互聯(lián)公司長期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為海寧企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè),海寧網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
Thymeleaf是一個表現(xiàn)層的模板引擎,一般被使用在Web環(huán)境中。
Thymeleaf支持多種模板類型:HTML、XML、JavaScript、CSS和普通文本等,默認(rèn)提供了6種模板處理模式:HTML、XML、TEXT、JAVASCRIPT 、CSS和RAW。最后一個RAW表示不會對模板進行處理。
開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一個名稱為demo的Spring Boot項目。
pom.xml加入Thymeleaf依賴:
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency>
一、處理HTML
新建一個類HtmlTest.java
package com.example.demo; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; public class HtmlTest { public static void main(String[] args){ TemplateEngine templateEngine = new TemplateEngine(); Context ctx = new Context(); String html = "<input type='' th:value='lc' />"; String result = templateEngine.process(html, ctx); System.out.println(result); } }
右鍵Run 'XmlTest.main()',控制臺輸出:
<input type='' value='lc' />
根據(jù)輸出可知,TemplateEngine類會將含有Thymeleaf邏輯的HTML代碼轉(zhuǎn)換成純HTML輸出,這就是模板引擎的作用。
二、更換模板解析器
默認(rèn)情況下,模板處理模式為HTML,可以設(shè)置不同的解析器。
新建一個類XmlTest.java
package com.example.demo; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.StringTemplateResolver; public class XmlTest { public static void main(String[] args){ TemplateEngine templateEngine = new TemplateEngine(); //新建模板解析器 StringTemplateResolver resolver = new StringTemplateResolver(); //設(shè)置模板模式為XML resolver.setTemplateMode(TemplateMode.XML); //將解析器設(shè)置到引擎實例中 templateEngine.setTemplateResolver(resolver); Context ctx = new Context(); String xml = "<bean id=\"bean1\" th:attr='class=com.example.demo'></bean>"; String result = templateEngine.process(xml, ctx); System.out.println(result); } }
右鍵Run 'HtmlTest.main()',控制臺輸出:
<bean id="bean1" class="com.example.demo"></bean>
三、處理資源文件
可以為模板引擎設(shè)置資源解析器,讓它去找文件進行處理。
1、在src/main/resources下新建文件index.html,只保留下面一行代碼
<input type='' th:value='lc' />
2、新建一個類 ClassLoaderTest.java
package com.example.demo; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; public class ClassLoaderTest { public static void main(String[] args) { TemplateEngine templateEngine = new TemplateEngine(); //創(chuàng)建解析器 ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); //將解析器設(shè)置到引擎實例中 templateEngine.setTemplateResolver(resolver); //處理classpath下的index.html String result = templateEngine.process("index.html", new Context()); System.out.println(result); } }
右鍵Run 'ClassLoaderTest.main()',控制臺輸出:
<input type='' value='lc' />
四、變量處理
可以通過Context實例設(shè)置變量值。
1、在src/main/resources下新建文件var.html
<input type='' th:value='${userName}' />
2、新建一個類 VarTest.java
package com.example.demo; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; public class VarTest { public static void main(String[] args) { TemplateEngine templateEngine = new TemplateEngine(); ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); templateEngine.setTemplateResolver(resolver); Context ctx = new Context(); ctx.setVariable("userName", "lc"); String result = templateEngine.process("var.html", ctx); System.out.println(result); } }
右鍵Run 'VarTest.main()',控制臺輸出:
<input type='' value='lc' />
五、遍歷集合
1、在src/main/resources下新建文件iteration.html
<table> <tr th:each="data : ${datas}"> <td th:text="${data}">姓名</td> </tr> </table>
2、新建一個類 IterationTest.java
package com.example.demo; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; import java.util.ArrayList; import java.util.List; public class IterationTest { public static void main(String[] args) { TemplateEngine templateEngine = new TemplateEngine(); ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); templateEngine.setTemplateResolver(resolver); List<String> datas = new ArrayList<String>(); datas.add("張三"); datas.add("李四"); Context ctx = new Context(); ctx.setVariable("datas", datas); ctx.setVariable("username", "lc"); String result = templateEngine.process("iteration.html", ctx); System.out.println(result); } }
右鍵Run 'IterationTest.main()',控制臺輸出:
<table> <tr> <td>張三</td> </tr> <tr> <td>李四</td> </tr> </table>
六、設(shè)置前綴與后綴
1、在src/main/resources/templates下新建文件index.html
<input type='' th:value='lc' />
2、新建一個類 PrefixSuffixTest.java
package com.example.demo; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; public class PrefixSuffixTest { public static void main(String[] args) { TemplateEngine templateEngine = new TemplateEngine(); ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); resolver.setPrefix("/templates/"); resolver.setSuffix(".html"); templateEngine.setTemplateResolver(resolver); String result = templateEngine.process("index", new Context()); System.out.println(result); } }
右鍵Run 'PrefixSuffixTest.main()',控制臺輸出:
<input type='' value='lc' />
七、Spring Boot中整合Thymeleaf
1、pom.xml加入Thymeleaf依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、application.yml
備注,下面配置的值都為默認(rèn)值,實際上可不配置。
spring: thymeleaf: mode: HTML prefix: classpath:/templates/ suffix: .html
3、在src/main/resources/templates下新建文件test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type='' th:value='${userName}' /> </body> </html>
4、新建一個控制器類 DemoController.java
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class DemoController { @RequestMapping("/test") public String test(Model model){ model.addAttribute("userName", "lc"); return "test"; } }
運行后,瀏覽器訪問http://localhost:8080/test
可看到輸出結(jié)果。
以上就是關(guān)于“怎么用Thymeleaf創(chuàng)建Spring Boot項目”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:怎么用Thymeleaf創(chuàng)建SpringBoot項目
URL網(wǎng)址:http://www.chinadenli.net/article10/pejodo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、靜態(tài)網(wǎng)站、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、軟件開發(fā)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)