在《Spring Boot 与 Kotlin 使用Thymeleaf模板引擎渲染web视图》一文中,我们使用Thymeleaf模板引擎渲染web视图,体验了kotlin 与spring boot结合是相当好的,这篇文章中继续介绍web视图,但是是使用Freemarker模板引擎渲染web视图。
Web相关的介绍这里就不多阐述,还没了解的请移步《Spring Boot 与 Kotlin 使用Thymeleaf模板引擎渲染web视图》
FreeMarker
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
FreeMarker是免费的,基于Apache许可证2.0版本发布。其模板编写为 FreeMarker
Template
Language
(FTL
)
,属于简单、专用的语言。需要准备数据在真实编程语言中来显示,比如数据库查询和业务运算, 之后模板显示已经准备好的数据。在模板中,主要用于如何展现数据, 而在模板之外注意于要展示什么数据 。
基本语法:
-
${...}:FreeMarker将会输出真实的值来替换大括号内的表达式,这样的表达式被称为interpolation(插值)。
-
注释:注释和HTML的注释也很相似,但是它们使用<#-- and -->来标识。不像HTML注释那样,FTL注释不会出现在输出中(不出现在访问者的页面中),因为FreeMarker会跳过它们。
-
FTL标签(FreeMarker模板的语言标签):FTL标签和HTML标签有一些相似之处,但是它们是FreeMarker的指令,是不会在输出中打印的。这些标签的名字以#开头。(用户自定义的FTL标签则需要使用@来代替#)
模版实例
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${username}!</h1>
<p>Our latest product:
<a href="${latestProduct.url}">${latestProduct.name}</a>!
</body>
</html>
在Spring Boot中使用 FreeMarker相关的,只需要引入下面依赖,并在默认的模板路径 src
/main
/resources
/templates下编写模板文件即可完成。
-
compile
"org.springframework.boot:spring-boot-starter-freemarker:$spring_boot_version"
完整的 build.gradle
-
group
'name.quanke.kotlin'
-
version
'1.0-SNAPSHOT'
-
-
buildscript
{
-
ext
.kotlin_version =
'1.2.10'
-
ext
.spring_boot_version =
'1.5.4.RELEASE'
-
repositories
{
-
mavenCentral
()
-
}
-
dependencies
{
-
classpath
"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
-
classpath
("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
)
-
-
// Kotlin整合SpringBoot的默认无参构造函数,默认把所有的类设置open类插件
-
classpath
("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
)
-
classpath
("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
)
-
}
-
}
-
-
apply plugin
: 'kotlin'
-
apply plugin
: "kotlin-spring"
// See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
-
apply plugin
: 'org.springframework.boot'
-
-
jar
{
-
baseName
= 'chapter11-5-2-service'
-
version
= '0.1.0'
-
}
-
repositories
{
-
mavenCentral
()
-
}
-
-
-
dependencies
{
-
compile
"org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
-
compile
"org.springframework.boot:spring-boot-starter-web:$spring_boot_version"
-
compile
"org.springframework.boot:spring-boot-starter-freemarker:$spring_boot_version"
-
testCompile
"org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
-
testCompile
"org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
-
-
}
-
-
compileKotlin
{
-
kotlinOptions
.jvmTarget =
"1.8"
-
}
-
compileTestKotlin
{
-
kotlinOptions
.jvmTarget =
"1.8"
-
}
举个例子:通过FreeMarker渲染一个页面。
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
/**
* Created by http://quanke.name on 2018/1/10.
*/
@Controller
class HelloController {
@RequestMapping("/")
fun index(model: Model): String {
// / 加入一个属性,用来在模板中读取
model.addAttribute("host", "http://quanke.name")
// return模板文件的名称,对应src/main/resources/templates/index.ftl
return "index"
}
}
默认在 src/main/resources/templates目录下增加 index
.ftl文件
注意Freemarker模版的后缀默认是
ftl,Thymeleaf模版后缀默认是html
<!DOCTYPE html>
<html>
<body>
<h4>我的博客:${host}!</h4>
<h1>quanke.name</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)
-
}
在 application.yml文中增加:
-
#设定ftl文件路径
-
spring
:
-
freemarker
:
-
template-loader
-path:
classpath:/templates
如上页面,直接打开html页面展现Hello World,但是启动程序后,访问http://localhost:8080/,则是展示Controller中host的值:http://quanke.name,做到了不破坏HTML自身内容的数据逻辑分离。
更多 FreeMarker相关的,还请访问 FreeMarker 官网 查询使用。
参考
-
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-freemarker
《Spring Boot 与 kotlin 实战》系列:
使用Spring Boot和Kotlin创建RESTfull API
Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图
全科龙婷▼升职加薪
▼