一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法

Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法

2021-03-22 14:38quanke Java教程

這篇文章主要介紹了Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下

本篇給大家介紹Spring Bootkotlin 使用Thymeleaf模板引擎渲染web視圖。

靜態(tài)資源訪問

在我們開發(fā)Web應(yīng)用的時候,需要引用大量的js、css、圖片等靜態(tài)資源,使用Spring Boot 與 kotlin如何去支持這些靜態(tài)資源?,很簡單。

默認(rèn)配置

Spring Boot默認(rèn)提供靜態(tài)資源目錄位置需置于 classpath 下,目錄名需符合如下規(guī)則:

?
1
2
3
4
/static
/public
/resources
/META-INF/resources

舉例:我們可以在src/main/resources/目錄下創(chuàng)建static,在該位置放置一個圖片文件。啟動程序后,嘗試訪問 http://localhost:8080/ruby.jpg 。如能顯示圖片,配置成功。

渲染W(wǎng)eb頁面

之前通過 @RestController 處理請求,返回的內(nèi)容為json對象。如果需要渲染 html 頁面,要如何實現(xiàn)呢?

模板引擎

在 Spring Boot 推薦的模板引擎下,我們可以很快的上手開發(fā)動態(tài)網(wǎng)站。

Spring Boot 提供了默認(rèn)配置的模板引擎主要有以下幾種:

?
1
2
3
4
Thymeleaf
FreeMarker
Groovy
Mustache

Spring Boot建議使用這些模板引擎,避免使用JSP,若一定要使用JSP將無法實現(xiàn)Spring Boot的多種特性,具體可見后文:支持JSP的配置

當(dāng)你使用上述模板引擎中的任何一個,它們默認(rèn)的模板配置路徑為: src/main/resources/templates 。當(dāng)然也可以修改這個路徑,具體如何修改,可在后續(xù)各模板引擎的配置屬性中查詢并修改。

Thymeleaf

Thymeleaf 是一個 XML/XHTML/HTML5 模板引擎,可用于Web與非Web環(huán)境中的應(yīng)用開發(fā)。它是一個開源的Java庫,基于Apache License 2.0許可,由Daniel Fernández創(chuàng)建,該作者還是Java加密庫Jasypt的作者。

Thymeleaf提供了一個用于整合Spring MVC的可選模塊,在應(yīng)用開發(fā)中,你可以使用Thymeleaf來完全代替JSP或其他模板引擎,如FreeMarker等。Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模。你可以使用它創(chuàng)建經(jīng)過驗證的XML與HTML模板。相對于編寫邏輯或代碼,開發(fā)者只需將標(biāo)簽屬性添加到模板中即可。接下來,這些標(biāo)簽屬性就會在DOM(文檔對象模型)上執(zhí)行預(yù)先制定好的邏輯。

示例模板:

?
1
2
3
4
5
6
7
8
9
10
<!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時,當(dāng)檢查到?jīng)]有的屬性時候會忽略,所以Thymeleaf的模板可以通過瀏覽器直接打開展現(xiàn),這樣非常有利于前后端的分離。

在Spring Boot中使用Thymeleaf,只需要引入下面依賴,并在默認(rèn)的模板路徑src/main/resources/templates下編寫模板文件即可完成。

?
1
compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"

在完成配置之后,舉一個簡單的例子,在快速入門工程的基礎(chǔ)上,舉一個簡單的示例來通過Thymeleaf渲染一個頁面。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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模板文件的名稱,對應(yīng)src/main/resources/templates/index.html
 return "index"
 }
}

默認(rèn)在 src/main/resources/templates 目錄下增加 index.html 文件

?
1
2
3
4
5
6
7
8
9
10
<!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 啟動方法:

?
1
2
3
4
5
6
7
8
9
10
11
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自身內(nèi)容的數(shù)據(jù)邏輯分離。

更多 Thymeleaf 的頁面語法,還請訪問Thymeleaf的官方文檔查詢使用。

Thymeleaf的默認(rèn)參數(shù)配置

如有需要修改默認(rèn)配置的時候,只需復(fù)制下面要修改的屬性到 application.yml 中,并修改成需要的值,如修改模板文件的擴展名,修改默認(rèn)的模板路徑等。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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)不可預(yù)期問題一般設(shè)置: spring.thymeleaf.cache=true

支持JSP的配置

Spring Boot并不建議使用,但如果一定要使用,可以參考此工程作為腳手架: JSP 支持

總的來說Kotlin 對于Spring Boot的支持非常好,只需要把Java語言的spring boot使用,翻譯成kotlin就可以。

總結(jié)

以上所述是小編給大家介紹的Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:https://www.jianshu.com/p/884632f71bc7

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产剧情在线观看 | 四虎影视永久在线精品免费 | 好湿好紧太硬了我太爽了网站 | 亚洲精品国产精麻豆久久99 | pppd在线播放| 天天快乐在线观看 | 四虎国产精品免费入口 | 91影视永久福利免费观看 | 热巴在公交车h文 | 天天爱天天做天天爽天天躁 | xx×日本老太 | 国产精品中文字幕 | 特黄aa级毛片免费视频播放 | 天天色综合色 | 色亚洲视频| 14一15sexvideo日本| 午夜精品国产 | 亚洲va久久久久综合 | 亚洲欧美成人中文在线网站 | 日本视频在线免费播放 | 亚洲色图中文字幕 | 亚洲精品久久久久69影院 | 痴mu动漫成年动漫在线观看 | yellow视频免费观看播放 | avtt在线观看| 亚洲AV久久无码精品九九软件 | 西西人体大胆77777视频 | 国产亚洲精品美女2020久久 | 亚洲精品网址 | 国产老肥熟xxxx | 亚洲酒色1314狠狠做 | 日韩亚洲国产欧美精品 | 男人的影院| 四虎免费影院在线播放 | 国产精品日本一区二区不卡视频 | 好男人在线观看免费高清2019韩剧 | 91国产高清| 日韩在线第一区 | 福利一区三区 | 爽好紧别夹宝贝叫大声点护士 | 亚洲99久久无色码中文字幕 |