精品乱人伦一区二区三区 ,a片试看120分钟做受视频红杏,国产乱码一区二区三区,亚洲国产欧美国产第一区

?

模板引擎翻譯 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

  • 法國簽證營業(yè)執(zhí)照翻譯件模板 你與申根簽證只有一條推送的距離
  • 江蘇省增值稅發(fā)票翻譯模板 江蘇稅務局出口貨物退(免)稅申報管理系統(tǒng)軟件
  • 肄業(yè)證書翻譯模板 復旦大學學生學業(yè)證明文書管理細則(試行)
  • 四級英語作文模板帶翻譯 大學英語四級翻譯模擬訓練及答案
  • 社會某信用代碼證翻譯模板 js驗證某社會信用代碼,某社會信用代碼 驗證js,js+驗證+社會信用代碼證
  • 美國移民證件翻譯模板 日語簽證翻譯聊聊身份證翻譯模板
  • 翻譯軟件模板 人類史上*實用的的文檔快速翻譯指南
  • 江蘇省增值稅發(fā)票翻譯模板 江蘇出口貨物退(免)稅申報管理服務平臺
  • 瑞士簽證房產證翻譯件模板 瑞士探親簽證—就讀子女
  • 日語戶口本翻譯模板 戶口本翻譯價格_戶口本翻譯一般多少錢?
  • 模板引擎翻譯 Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染we www.www.amdcu.cn/fymb/8734.html
    ?
    本站部分內容和圖片來源于網絡用戶和讀者投稿,不確定投稿用戶享有完全著作權,根據(jù)《信息網絡傳播權保護條例》,如果侵犯了您的權利,請聯(lián)系:chinazxzy@163.com,及時刪除。
    Go To Top 回頂部
    • 掃一掃,微信在線