温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot与kotlin如何使用Thymeleaf模板引擎渲染web视图

发布时间:2021-08-05 15:27:02 来源:亿速云 阅读:125 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关Spring Boot与kotlin如何使用Thymeleaf模板引擎渲染web视图的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

静态资源访问

在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源,使用Spring Boot 与 kotlin如何去支持这些静态资源?,很简单。

默认配置

Spring Boot默认提供静态资源目录位置需置于 classpath 下,目录名需符合如下规则:

/static
/public
/resources
/META-INF/resources

举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问 http://localhost:8080/ruby.jpg 。如能显示图片,配置成功。

渲染Web页面

之前通过 @RestController 处理请求,返回的内容为json对象。如果需要渲染 html 页面,要如何实现呢?

模板引擎

在 Spring Boot 推荐的模板引擎下,我们可以很快的上手开发动态网站。

Spring Boot 提供了默认配置的模板引擎主要有以下几种:

Thymeleaf
FreeMarker
Groovy
Mustache

Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性,具体可见后文:支持JSP的配置

当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为: src/main/resources/templates 。当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。

Thymeleaf

Thymeleaf 是一个 XML/XHTML/HTML5 模板引擎,可用于Web与非Web环境中的应用开发。它是一个开源的Java库,基于Apache License 2.0许可,由Daniel Fernández创建,该作者还是Java加密库Jasypt的作者。

Thymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP或其他模板引擎,如FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。

示例模板:

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
 <meta charset="UTF-8" />
 <title>quanke.name</title>
</head>
<body>
<h2 th:text="${host}">Hello World</h2>
</body>
</html>

可以看到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 文件

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
 <meta charset="UTF-8" />
 <title>quanke.name</title>
</head>
<body>
<h2 th:text="${host}">Hello World</h2>
</body>
</html>

增加使用 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<String>) {
 SpringApplication.run(Application::class.java, *args)
}

如上页面,直接打开html页面展现Hello World,但是启动程序后,访问 http://localhost:8080/ ,则是展示Controller中host的值:http://quanke.name,做到了不破坏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 支持

总的来说Kotlin 对于Spring Boot的支持非常好,只需要把Java语言的spring boot使用,翻译成kotlin就可以。

感谢各位的阅读!关于“Spring Boot与kotlin如何使用Thymeleaf模板引擎渲染web视图”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI