Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
本篇給大家介紹Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖。
靜態(tài)資源訪問
在我們開發(fā)Web應用的時候,需要引用大量的js、css、圖片等靜態(tài)資源,使用Spring Boot 與 kotlin如何去支持這些靜態(tài)資源?,很簡單。
默認配置
Spring Boot默認提供靜態(tài)資源目錄位置需置于 classpath 下,目錄名需符合如下規(guī)則:
/static /public /resources /META-INF/resources
舉例:我們可以在src/main/resources/目錄下創(chuàng)建static,在該位置放置一個圖片文件。啟動程序后,嘗試訪問 http://localhost:8080/ruby.jpg 。如能顯示圖片,配置成功。
渲染Web頁面
之前通過 @RestController 處理請求,返回的內容為json對象。如果需要渲染 html 頁面,要如何實現(xiàn)呢?
模板引擎
在 Spring Boot 推薦的模板引擎下,我們可以很快的上手開發(fā)動態(tài)網站。
Spring Boot 提供了默認配置的模板引擎主要有以下幾種:
Thymeleaf FreeMarker Groovy Mustache
Spring Boot建議使用這些模板引擎,避免使用JSP,若一定要使用JSP將無法實現(xiàn)Spring Boot的多種特性,具體可見后文:支持JSP的配置
當你使用上述模板引擎中的任何一個,它們默認的模板配置路徑為: src/main/resources/templates 。當然也可以修改這個路徑,具體如何修改,可在后續(xù)各模板引擎的配置屬性中查詢并修改。
Thymeleaf
Thymeleaf 是一個 XML/XHTML/HTML5 模板引擎,可用于Web與非Web環(huán)境中的應用開發(fā)。它是一個開源的Java庫,基于Apache License 2.0許可,由Daniel Fernández創(chuàng)建,該作者還是Java加密庫Jasypt的作者。
Thymeleaf提供了一個用于整合Spring MVC的可選模塊,在應用開發(fā)中,你可以使用Thymeleaf來完全代替JSP或其他模板引擎,如FreeMarker等。Thymeleaf的主要目標在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模。你可以使用它創(chuàng)建經過驗證的XML與HTML模板。相對于編寫邏輯或代碼,開發(fā)者只需將標簽屬性添加到模板中即可。接下來,這些標簽屬性就會在DOM(文檔對象模型)上執(zhí)行預先制定好的邏輯。
示例模板:
<!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標簽中,瀏覽器在解析html時,當檢查到沒有的屬性時候會忽略,所以Thymeleaf的模板可以通過瀏覽器直接打開展現(xiàn),這樣非常有利于前后端的分離。
在Spring Boot中使用Thymeleaf,只需要引入下面依賴,并在默認的模板路徑src/main/resources/templates下編寫模板文件即可完成。
compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"
在完成配置之后,舉一個簡單的例子,在快速入門工程的基礎上,舉一個簡單的示例來通過Thymeleaf渲染一個頁面。
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 {
// / 加入一個屬性,用來在模板中讀取
map.addAttribute("host", "http://quanke.name")
// return模板文件的名稱,對應src/main/resources/templates/index.html
return "index"
}
}
默認在 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 語言實現(xiàn)的 Spring Boot 啟動方法:
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)
}
如上頁面,直接打開html頁面展現(xiàn)Hello World,但是啟動程序后,訪問 http://localhost:8080/ ,則是展示Controller中host的值:http://quanke.name,做到了不破壞HTML自身內容的數(shù)據邏輯分離。
更多 Thymeleaf 的頁面語法,還請訪問Thymeleaf的官方文檔查詢使用。
Thymeleaf的默認參數(shù)配置
如有需要修改默認配置的時候,只需復制下面要修改的屬性到 application.yml 中,并修改成需要的值,如修改模板文件的擴展名,修改默認的模板路徑等。
# 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.
測試環(huán)境或者開發(fā)環(huán)境避免出現(xiàn)不可預期問題一般設置: spring.thymeleaf.cache=true
支持JSP的配置
Spring Boot并不建議使用,但如果一定要使用,可以參考此工程作為腳手架: JSP 支持
總的來說Kotlin 對于Spring Boot的支持非常好,只需要把Java語言的spring boot使用,翻譯成kotlin就可以。
總結
以上所述是小編給大家介紹的Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
spring boot使用properties定義短信模板的方法教程
這篇文章主要給大家介紹了關于spring boot使用properties定義短信模板的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01
SpringBoot配置自定義攔截器實現(xiàn)過程詳解
在系統(tǒng)中經常需要在處理用戶請求之前和之后執(zhí)行一些行為,例如檢測用戶的權限,或者將請求的信息記錄到日志中,即平時所說的"權限檢測"及"日志記錄",下面這篇文章主要給大家介紹了關于在SpringBoot項目中整合攔截器的相關資料,需要的朋友可以參考下2022-10-10
詳解Java如何實現(xiàn)在PDF中插入,替換或刪除圖像
圖文并茂的內容往往讓人看起來更加舒服,如果只是文字內容的累加,往往會使讀者產生視覺疲勞。搭配精美的文章配圖則會使文章內容更加豐富。那我們要如何在PDF中插入、替換或刪除圖像呢?別擔心,今天為大家介紹一種高效便捷的方法2023-01-01
SpringBoot中@ControllerAdvice注解的使用方法
這篇文章主要介紹了SpringBoot中@ControllerAdvice注解的使用方法,這是一個增強的?Controller,對controller層做異常處理、數(shù)據預處理、全局數(shù)據綁定,?springboot?會自動掃描到,不需要調用,這個注解是spring?MVC提供的,在springboot中也可以使用,需要的朋友可以參考下2024-01-01
java實現(xiàn)excel導入數(shù)據的工具類
這篇文章主要介紹了java實現(xiàn)的excel導入數(shù)據的工具類,需要的朋友可以參考下2014-03-03
springboot Interceptor攔截器excludePathPatterns忽略失效
這篇文章主要介紹了springboot Interceptor攔截器excludePathPatterns忽略失效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

