模板引擎翻譯 Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染we
日期:2023-03-11 12:39:36 / 人氣: 478 / 發(fā)布者:成都翻譯公司
使用Thymeleaf模板引擎渲染web視圖。如果需要渲染html頁面,要如何實現(xiàn)呢?當你使用上述模板引擎中的任何一個,它們默認的模板配置路徑為:src/main/resources/templates。MVC的可選模塊,在應用開發(fā)中,你可以使用Thymeleaf來完全代替JSP或其他模板引擎,如FreeMarker等。boot使用,翻譯成kotlin就可以。《Kotlin與Spring Boot實戰(zhàn)》QQ群:370156529 在《用Spring Boot與Kotlin創(chuàng)建RESTfull API》一文中,我們完成了一個簡單的RESTful服務,體驗了Spring Boot與Kotlin結合的威力,但往往也需要web的支持,那么這篇文章在上一篇文章的基礎上介紹了Spring Boot和Kotlin使用Thymeleaf模板引擎渲染web視圖。靜態(tài)資源訪問
我們在開發(fā)web應用的時候,需要引用很多靜態(tài)資源,比如js、css、圖片等,我們如何使用Spring Boot和kotlin來支持這些靜態(tài)資源呢?,很簡單。
默認分配
Spring Boot默認在classpath下提供靜態(tài)資源目錄位置,目錄名必須符合以下規(guī)則:
/static
/public
/resources
/META-INF/resources
示例:我們可以在 src/main/resources/ 目錄下創(chuàng)建 static 并在此位置放置一個圖像文件。啟動程序后模板引擎翻譯,嘗試訪問:8080/ruby.jpg。如果可以顯示圖片,則配置成功。呈現(xiàn)網頁
之前通過@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 是一個 XML/XHTML/HTML5 模板引擎,可用于 Web 和非 Web 環(huán)境中的應用程序開發(fā)。它是一個開源 Java 庫,基于 Apache License 2.0 license,由 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í)行預定義的邏輯。
示例模板:
quanke.name
Hello World
可以看出,Thymeleaf主要是以屬性的形式添加到html標簽中的。當瀏覽器解析html時,當它檢查沒有屬性時會忽略它。所以Thymeleaf模板可以直接通過瀏覽器打開顯示,對前后端非常有利。分離。
在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文件
quanke.name
Hello World
添加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) {
SpringApplication.run(Application::class.java, *args)
}
如上頁面,直接打開html頁面顯示Hello World,但是啟動程序后訪問:8080/,是在Controller:中顯示host的值,不破壞數(shù)據(jù)邏輯分離HTML 本身。
更多 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 support
總的來說,Kotlin 對 Spring Boot 的支持是非常好的。只需要使用Java語言的spring boot,翻譯成kotlin即可。
《Kotlin與Spring Boot實戰(zhàn)》QQ群:370156529
相關閱讀Relate
熱門文章 Recent
- 身份證翻譯模板acca 關于如何報考ACCA證書2023-03-11
- 會議日程表模板翻譯 企業(yè)級WinForm界面控件集2023-03-11
- 營業(yè)執(zhí)照證件翻譯件模板 營業(yè)執(zhí)照翻譯件怎么辦理?2023-03-11
- 基于模板的統(tǒng)計翻譯 測繪科學雜志2020年第08期一種阿拉伯語地名的機器翻譯方法2023-03-11
- 上海市居住證翻譯模板 有居住證的非滬籍子女可以在上海參加高考嗎?2023-03-11
- 大學自我介紹英語模板翻譯 英文簡歷自我介紹怎么寫?2023-03-11
- 銀行對賬單翻譯模板 銀行流水賬單翻譯技巧2023-03-11
- 銀行詢證函翻譯模板 銀行詢證函是什么意思|銀行詢證函是什么?開具銀行詢證函應該注意的幾個地方2023-03-11
- 翻譯實踐報告開題報告模板 外文翻譯開題報告要求及指導意見2023-03-11
- 翻譯評鑒模板 英語翻譯崗位自我評價范文2023-03-11