当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法

Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

非法操作,熟女4050,吝啬王爷贪财妃

本篇给大家介绍spring boot 与 kotlin 使用thymeleaf模板引擎渲染web视图。

静态资源访问

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

默认配置

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

/static
/public
/resources
/meta-inf/resources

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

渲染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>
<h1 th:text="${host}">hello world</h1>
</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/
 return "index"
 }
}

默认在 src/main/resources/templates 目录下增加 文件

<!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 语言实现的 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,但是启动程序后,访问 ,则是展示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 支持

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

总结

以上所述是小编给大家介绍的spring boot 与 kotlin 使用thymeleaf模板引擎渲染web视图的方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网