模板引擎翻譯 Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染we
日期:2023-03-11 12:39:36 / 人氣: 478 / 發(fā)布者:成都翻譯公司
使用Thymeleaf模板引擎渲染web視圖。如果需要渲染html頁(yè)面,要如何實(shí)現(xiàn)呢?當(dāng)你使用上述模板引擎中的任何一個(gè),它們默認(rèn)的模板配置路徑為:src/main/resources/templates。MVC的可選模塊,在應(yīng)用開(kāi)發(fā)中,你可以使用Thymeleaf來(lái)完全代替JSP或其他模板引擎,如FreeMarker等。boot使用,翻譯成kotlin就可以。《Kotlin與Spring Boot實(shí)戰(zhàn)》QQ群:370156529 在《用Spring Boot與Kotlin創(chuàng)建RESTfull API》一文中,我們完成了一個(gè)簡(jiǎn)單的RESTful服務(wù),體驗(yàn)了Spring Boot與Kotlin結(jié)合的威力,但往往也需要web的支持,那么這篇文章在上一篇文章的基礎(chǔ)上介紹了Spring Boot和Kotlin使用Thymeleaf模板引擎渲染web視圖。靜態(tài)資源訪問(wèn)
我們?cè)陂_(kāi)發(fā)web應(yīng)用的時(shí)候,需要引用很多靜態(tài)資源,比如js、css、圖片等,我們?nèi)绾问褂肧pring Boot和kotlin來(lái)支持這些靜態(tài)資源呢?,很簡(jiǎn)單。
默認(rèn)分配
Spring Boot默認(rèn)在classpath下提供靜態(tài)資源目錄位置,目錄名必須符合以下規(guī)則:
/static
/public
/resources
/META-INF/resources
示例:我們可以在 src/main/resources/ 目錄下創(chuàng)建 static 并在此位置放置一個(gè)圖像文件。啟動(dòng)程序后模板引擎翻譯,嘗試訪問(wèn):8080/ruby.jpg。如果可以顯示圖片,則配置成功。呈現(xiàn)網(wǎng)頁(yè)
之前通過(guò)@RestController處理請(qǐng)求,返回的內(nèi)容是一個(gè)json對(duì)象。如果需要渲染一個(gè)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 推薦使用這些模板引擎來(lái)避免使用 JSP。如果一定要使用JSP,就無(wú)法實(shí)現(xiàn)Spring Boot的各種特性。詳情請(qǐng)看下文:支持JSP配置
當(dāng)您使用上述任一模板引擎時(shí),它們的默認(rèn)模板配置路徑為:src/main/resources/templates。當(dāng)然你也可以修改這個(gè)路徑,具體如何修改可以在后續(xù)模板引擎的配置屬性中查詢修改。
百里香葉
Thymeleaf 是一個(gè) XML/XHTML/HTML5 模板引擎,可用于 Web 和非 Web 環(huán)境中的應(yīng)用程序開(kāi)發(fā)。它是一個(gè)開(kāi)源 Java 庫(kù),基于 Apache License 2.0 license,由 Daniel Fernández 創(chuàng)建,作者也是 Java 加密庫(kù) Jasypt 的作者。
Thymeleaf 提供了一個(gè)可選的模塊來(lái)集成 Spring MVC。在應(yīng)用開(kāi)發(fā)中,可以使用 Thymeleaf 來(lái)完全替代 JSP 或其他模板引擎,如 FreeMarker。Thymeleaf 的主要目標(biāo)是提供一種格式良好的模板創(chuàng)建方法,可以被瀏覽器正確顯示,因此它也可以用于靜態(tài)建模。您可以使用它來(lái)創(chuàng)建經(jīng)過(guò)驗(yàn)證的 XML 和 HTML 模板。與編寫邏輯或代碼相比,開(kāi)發(fā)人員只需在模板中添加標(biāo)簽屬性即可。接下來(lái),這些標(biāo)簽屬性將在 DOM(文檔對(duì)象模型)上執(zhí)行預(yù)定義的邏輯。
示例模板:
quanke.name
Hello World
可以看出,Thymeleaf主要是以屬性的形式添加到html標(biāo)簽中的。當(dāng)瀏覽器解析html時(shí),當(dāng)它檢查沒(méi)有屬性時(shí)會(huì)忽略它。所以Thymeleaf模板可以直接通過(guò)瀏覽器打開(kāi)顯示,對(duì)前后端非常有利。分離。
在Spring Boot中使用Thymeleaf,只需要引入如下依賴,并在默認(rèn)模板路徑src/main/resources/templates下編寫一個(gè)模板文件即可。
compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"
配置完成后,舉個(gè)簡(jiǎn)單的例子,基于快速入門項(xiàng)目模板引擎翻譯,舉個(gè)簡(jiǎn)單的例子,通過(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文件
quanke.name
Hello World
添加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) {
SpringApplication.run(Application::class.java, *args)
}
如上頁(yè)面,直接打開(kāi)html頁(yè)面顯示Hello World,但是啟動(dòng)程序后訪問(wèn):8080/,是在Controller:中顯示host的值,不破壞數(shù)據(jù)邏輯分離HTML 本身。
更多 Thymeleaf 頁(yè)面語(yǔ)法,請(qǐng)?jiān)L問(wèn) Thymeleaf 官方文檔查詢。
Thymeleaf 的默認(rèn)參數(shù)配置
如果需要修改默認(rèn)配置,只需將下面需要修改的屬性復(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)意外問(wèn)題。一般設(shè)置:spring.thymeleaf.cache=true 支持JSP配置
Spring Boot 不推薦,但如果一定要使用,可以參考這個(gè)項(xiàng)目的腳手架:JSP support
總的來(lái)說(shuō),Kotlin 對(duì) Spring Boot 的支持是非常好的。只需要使用Java語(yǔ)言的spring boot,翻譯成kotlin即可。
《Kotlin與Spring Boot實(shí)戰(zhàn)》QQ群:370156529
相關(guān)閱讀Relate
熱門文章 Recent
- 翻譯項(xiàng)目總結(jié)模板 【翻譯】【項(xiàng)目架構(gòu)必備】Asp.Net MVC3 定義自己的項(xiàng)目模板2023-03-11
- 深圳市居住證翻譯模板 深圳居住證辦理要求與辦理流程是什么2023-03-11
- 房租收入翻譯模板 我喜歡的三種賺取被動(dòng)收入的方式2023-03-11
- 中專畢業(yè)證翻譯模板(專業(yè)的畢業(yè)證翻譯模板)2023-03-11
- 英語(yǔ)演講稿萬(wàn)能模板帶翻譯 帶翻譯英語(yǔ)演講稿萬(wàn)能模板5篇2023-03-11
- 高中英語(yǔ)作文模板和翻譯 2020高考英語(yǔ)作文模版2023-03-11
- 國(guó)外無(wú)犯罪記錄證明翻譯模板 派出所拒為英文版無(wú)犯罪記錄證明蓋章:看不懂(圖)2023-03-11
- 求翻譯模板樣本 翻譯服務(wù)合同書樣本一(示范合同)2023-03-11
- 信用證條款的翻譯模板 (外貿(mào)英語(yǔ)函電)項(xiàng)目八信用證修改和延展2023-03-11
- 銀行流水單翻譯模板百度 銀行流水打印及翻譯要求2023-03-11