Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
本篇給大家介紹Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖。
靜態(tài)資源訪問(wèn)
在我們開(kāi)發(fā)Web應(yīng)用的時(shí)候,需要引用大量的js、css、圖片等靜態(tài)資源,使用Spring Boot 與 kotlin如何去支持這些靜態(tài)資源?,很簡(jiǎn)單。
默認(rèn)配置
Spring Boot默認(rèn)提供靜態(tài)資源目錄位置需置于 classpath 下,目錄名需符合如下規(guī)則:
/static /public /resources /META-INF/resources
舉例:我們可以在src/main/resources/目錄下創(chuàng)建static,在該位置放置一個(gè)圖片文件。啟動(dòng)程序后,嘗試訪問(wèn) http://localhost:8080/ruby.jpg 。如能顯示圖片,配置成功。
渲染W(wǎng)eb頁(yè)面
之前通過(guò) @RestController 處理請(qǐng)求,返回的內(nèi)容為json對(duì)象。如果需要渲染 html 頁(yè)面,要如何實(shí)現(xiàn)呢?
模板引擎
在 Spring Boot 推薦的模板引擎下,我們可以很快的上手開(kāi)發(fā)動(dòng)態(tài)網(wǎng)站。
Spring Boot 提供了默認(rèn)配置的模板引擎主要有以下幾種:
Thymeleaf FreeMarker Groovy Mustache
Spring Boot建議使用這些模板引擎,避免使用JSP,若一定要使用JSP將無(wú)法實(shí)現(xiàn)Spring Boot的多種特性,具體可見(jiàn)后文:支持JSP的配置
當(dāng)你使用上述模板引擎中的任何一個(gè),它們默認(rèn)的模板配置路徑為: src/main/resources/templates 。當(dāng)然也可以修改這個(gè)路徑,具體如何修改,可在后續(xù)各模板引擎的配置屬性中查詢并修改。
Thymeleaf
Thymeleaf 是一個(gè) XML/XHTML/HTML5 模板引擎,可用于Web與非Web環(huán)境中的應(yīng)用開(kāi)發(fā)。它是一個(gè)開(kāi)源的Java庫(kù),基于Apache License 2.0許可,由Daniel Fernández創(chuàng)建,該作者還是Java加密庫(kù)Jasypt的作者。
Thymeleaf提供了一個(gè)用于整合Spring MVC的可選模塊,在應(yīng)用開(kāi)發(fā)中,你可以使用Thymeleaf來(lái)完全代替JSP或其他模板引擎,如FreeMarker等。Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模。你可以使用它創(chuàng)建經(jīng)過(guò)驗(yàn)證的XML與HTML模板。相對(duì)于編寫(xiě)邏輯或代碼,開(kāi)發(fā)者只需將標(biāo)簽屬性添加到模板中即可。接下來(lái),這些標(biāo)簽屬性就會(huì)在DOM(文檔對(duì)象模型)上執(zhí)行預(yù)先制定好的邏輯。
示例模板:
<!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head lang="en"> <meta charset="UTF-8" /> <title>quanke.name</title> </head> <body> <h1 th:text="${host}">Hello World</h1> </body> </html>
可以看到Thymeleaf主要以屬性的方式加入到html標(biāo)簽中,瀏覽器在解析html時(shí),當(dāng)檢查到?jīng)]有的屬性時(shí)候會(huì)忽略,所以Thymeleaf的模板可以通過(guò)瀏覽器直接打開(kāi)展現(xiàn),這樣非常有利于前后端的分離。
在Spring Boot中使用Thymeleaf,只需要引入下面依賴,并在默認(rèn)的模板路徑src/main/resources/templates下編寫(xiě)模板文件即可完成。
compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"
在完成配置之后,舉一個(gè)簡(jiǎn)單的例子,在快速入門工程的基礎(chǔ)上,舉一個(gè)簡(jiǎn)單的示例來(lái)通過(guò)Thymeleaf渲染一個(gè)頁(yè)面。
import org.springframework.stereotype.Controller import org.springframework.ui.ModelMap import org.springframework.web.bind.annotation.RequestMapping /** * Created by http://quanke.name on 2018/1/10. */ @Controller class HelloController { @RequestMapping("/") fun index(map: ModelMap): String { // / 加入一個(gè)屬性,用來(lái)在模板中讀取 map.addAttribute("host", "http://quanke.name") // return模板文件的名稱,對(duì)應(yīng)src/main/resources/templates/index.html return "index" } }
默認(rèn)在 src/main/resources/templates 目錄下增加 index.html 文件
<!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head lang="en"> <meta charset="UTF-8" /> <title>quanke.name</title> </head> <body> <h1 th:text="${host}">Hello World</h1> </body> </html>
增加使用 kotlin 語(yǔ)言實(shí)現(xiàn)的 Spring Boot 啟動(dòng)方法:
import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication /** * Created by http://quanke.name on 2018/1/9. */ @SpringBootApplication class Application fun main(args: Array<String>) { SpringApplication.run(Application::class.java, *args) }
如上頁(yè)面,直接打開(kāi)html頁(yè)面展現(xiàn)Hello World,但是啟動(dòng)程序后,訪問(wèn) http://localhost:8080/ ,則是展示Controller中host的值:http://quanke.name,做到了不破壞HTML自身內(nèi)容的數(shù)據(jù)邏輯分離。
更多 Thymeleaf 的頁(yè)面語(yǔ)法,還請(qǐng)?jiān)L問(wèn)Thymeleaf的官方文檔查詢使用。
Thymeleaf的默認(rèn)參數(shù)配置
如有需要修改默認(rèn)配置的時(shí)候,只需復(fù)制下面要修改的屬性到 application.yml 中,并修改成需要的值,如修改模板文件的擴(kuò)展名,修改默認(rèn)的模板路徑等。
# Enable template caching. spring.thymeleaf.cache=true # Check that the templates location exists. spring.thymeleaf.check-template-location=true # Content-Type value. spring.thymeleaf.content-type=text/html # Enable MVC Thymeleaf view resolution. spring.thymeleaf.enabled=true # Template encoding. spring.thymeleaf.encoding=UTF-8 # Comma-separated list of view names that should be excluded from resolution. spring.thymeleaf.excluded-view-names= # Template mode to be applied to templates. See also StandardTemplateModeHandlers. spring.thymeleaf.mode=HTML5 # Prefix that gets prepended to view names when building a URL. spring.thymeleaf.prefix=classpath:/templates/ # Suffix that gets appended to view names when building a URL. spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
測(cè)試環(huán)境或者開(kāi)發(fā)環(huán)境避免出現(xiàn)不可預(yù)期問(wèn)題一般設(shè)置: spring.thymeleaf.cache=true
支持JSP的配置
Spring Boot并不建議使用,但如果一定要使用,可以參考此工程作為腳手架: JSP 支持
總的來(lái)說(shuō)Kotlin 對(duì)于Spring Boot的支持非常好,只需要把Java語(yǔ)言的spring boot使用,翻譯成kotlin就可以。
總結(jié)
以上所述是小編給大家介紹的Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot生成jar/war包的布局應(yīng)用
在 Spring Boot 中,"布局應(yīng)用"(Application Layout)指的是打包生成的可執(zhí)行 jar 或 war 文件中的內(nèi)容組織結(jié)構(gòu),本文給大家介紹了SpringBoot生成jar/war包的布局應(yīng)用,需要的朋友可以參考下2024-02-02MyBatis中的SQL映射文件配置結(jié)果映射的操作指南
MyBatis?是一款優(yōu)秀的?ORM?框架,它提供了多種配置方式來(lái)定義?SQL?語(yǔ)句以及結(jié)果映射規(guī)則,本文將介紹?MyBatis?中的?SQL?映射文件如何配置結(jié)果映射,包括常規(guī)類型、集合類型等多種情況,需要的朋友可以參考下2023-07-07SpringBoot實(shí)現(xiàn)自定義注解用于文件驗(yàn)證的詳細(xì)過(guò)程(大小、擴(kuò)展名、MIME類型)
SpringBoot,Spring Cloud中經(jīng)常需要處理文件上傳的功能,為了確保上傳的文件滿足特定的要求(如擴(kuò)展名、MIME類型和文件大?。?我們可以創(chuàng)建一個(gè)自定義注解來(lái)簡(jiǎn)化驗(yàn)證過(guò)程,需要的朋友可以參考下2024-08-08Java中的數(shù)組流ByteArrayOutputStream用法
Java中的ByteArrayOutputStream是java.io包中的一個(gè)類,用于在內(nèi)存中創(chuàng)建字節(jié)數(shù)組緩沖區(qū),支持動(dòng)態(tài)擴(kuò)展,它繼承自O(shè)utputStream,允許以字節(jié)形式寫(xiě)入數(shù)據(jù),無(wú)需與外部設(shè)備交互,常用方法包括write()、toByteArray()、toString()等2024-09-09