模板引擎翻译 Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染we
日期:2023-03-11 12:39:36 / 人气: 508 / 发布者:成都翻译公司
使用Thymeleaf模板引擎渲染web视图。如果需要渲染html页面,要如何实现呢?当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP或其他模板引擎,如FreeMarker等。boot使用,翻译成kotlin就可以。《Kotlin与Spring Boot实战》QQ群:370156529 在《用Spring Boot与Kotlin创建RESTfull API》一文中,我们完成了一个简单的RESTful服务,体验了Spring Boot与Kotlin结合的威力,但往往也需要web的支持,那么这篇文章在上一篇文章的基础上介绍了Spring Boot和Kotlin使用Thymeleaf模板引擎渲染web视图。静态资源访问
我们在开发web应用的时候,需要引用很多静态资源,比如js、css、图片等,我们如何使用Spring Boot和kotlin来支持这些静态资源呢?,很简单。
默认分配
Spring Boot默认在classpath下提供静态资源目录位置,目录名必须符合以下规则:
/static
/public
/resources
/META-INF/resources
示例:我们可以在 src/main/resources/ 目录下创建 static 并在此位置放置一个图像文件。启动程序后模板引擎翻译,尝试访问:8080/ruby.jpg。如果可以显示图片,则配置成功。呈现网页
之前通过@RestController处理请求,返回的内容是一个json对象。如果需要渲染一个html页面,如何实现呢?
模板引擎
借助Spring Boot推荐的模板引擎,我们可以快速上手开发动态网站。
Spring Boot 提供了以下具有默认配置的模板引擎:
Thymeleaf
FreeMarker
Groovy
Mustache
Spring Boot 推荐使用这些模板引擎来避免使用 JSP。如果一定要使用JSP,就无法实现Spring Boot的各种特性。详情请看下文:支持JSP配置
当您使用上述任一模板引擎时,它们的默认模板配置路径为:src/main/resources/templates。当然你也可以修改这个路径,具体如何修改可以在后续模板引擎的配置属性中查询修改。
百里香叶
Thymeleaf 是一个 XML/XHTML/HTML5 模板引擎,可用于 Web 和非 Web 环境中的应用程序开发。它是一个开源 Java 库,基于 Apache License 2.0 license,由 Daniel Fernández 创建,作者也是 Java 加密库 Jasypt 的作者。
Thymeleaf 提供了一个可选的模块来集成 Spring MVC。在应用开发中,可以使用 Thymeleaf 来完全替代 JSP 或其他模板引擎,如 FreeMarker。Thymeleaf 的主要目标是提供一种格式良好的模板创建方法,可以被浏览器正确显示,因此它也可以用于静态建模。您可以使用它来创建经过验证的 XML 和 HTML 模板。与编写逻辑或代码相比,开发人员只需在模板中添加标签属性即可。接下来,这些标签属性将在 DOM(文档对象模型)上执行预定义的逻辑。
示例模板:
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语言实现的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的值,不破坏数据逻辑分离HTML 本身。
更多 Thymeleaf 页面语法,请访问 Thymeleaf 官方文档查询。
Thymeleaf 的默认参数配置
如果需要修改默认配置,只需将下面需要修改的属性复制到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.
测试环境或开发环境,避免出现意外问题。一般设置:spring.thymeleaf.cache=true 支持JSP配置
Spring Boot 不推荐,但如果一定要使用,可以参考这个项目的脚手架:JSP support
总的来说,Kotlin 对 Spring Boot 的支持是非常好的。只需要使用Java语言的spring boot,翻译成kotlin即可。
《Kotlin与Spring Boot实战》QQ群:370156529
相关阅读Relate
热门文章 Recent
- 新南威尔士大学学位证书翻译模板 新南威尔士大学翻译硕士2023-03-11
- 户口本翻译英文模板英文 申请出国签证是否需要翻译居民身份证户口本这些材料?2023-03-11
- 法国学校录取通知书翻译模板 2019-法国留学申请书(精选多篇)-推荐word版 (9页)2023-03-11
- 出国单位证明信翻译模板英汉对照 出国留学实习证明范本2023-03-11
- 感谢信翻译模板 感谢信英文的翻译(标准版)2023-03-11
- 木模板翻译 圆柱体定形建筑木模板工程施工方案2023-03-11
- 房产租赁翻译模板 要怎么写租房合同2023-03-11
- 加拿大签证结婚证户口翻译模板 签证攻略|办理加拿大签证需要的资料2023-03-11
- 海德堡大学学位证书翻译模板 【海德堡大学】德国UNIHEIDELBERG2023-03-11
- 翻译公司个人保密协议模板 2016年保密协议范本2023-03-11


