Spring Boot 与 Kotlin使用Freemarker模板引擎渲染web视图

301 阅读3分钟
原文链接: mp.weixin.qq.com

《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标签则需要使用@来代替#)

模版实例

  1. <html>

  2.    <head>

  3.        <title>Welcome!</title>

  4.    </head>

  5.    <body>

  6.        <h1>Welcome ${username}!</h1>

  7.        <p>Our latest product:

  8.        <a href="${latestProduct.url}">${latestProduct.name}</a>!

  9.    </body>

  10. </html>

在Spring Boot中使用 FreeMarker相关的,只需要引入下面依赖,并在默认的模板路径  src /main /resources /templates下编写模板文件即可完成。

                            
  1. compile "org.springframework.boot:spring-boot-starter-freemarker:$spring_boot_version"

完整的 build.gradle

                                
  1. group 'name.quanke.kotlin'

  2. version '1.0-SNAPSHOT'

  3. buildscript {

  4.    ext .kotlin_version = '1.2.10'

  5.    ext .spring_boot_version = '1.5.4.RELEASE'

  6.    repositories {

  7.        mavenCentral ()

  8.     }

  9.    dependencies {

  10.        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

  11.        classpath ("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version" )

  12. //        Kotlin整合SpringBoot的默认无参构造函数,默认把所有的类设置open类插件

  13.        classpath ("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version" )

  14.        classpath ("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" )

  15.     }

  16. }

  17. apply plugin : 'kotlin'

  18. apply plugin : "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin

  19. apply plugin : 'org.springframework.boot'

  20. jar {

  21.    baseName = 'chapter11-5-2-service'

  22.    version = '0.1.0'

  23. }

  24. repositories {

  25.    mavenCentral ()

  26. }

  27. dependencies {

  28.    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"

  29.    compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version"

  30.    compile "org.springframework.boot:spring-boot-starter-freemarker:$spring_boot_version"

  31.    testCompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"

  32.    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"

  33. }

  34. compileKotlin {

  35.    kotlinOptions .jvmTarget = "1.8"

  36. }

  37. compileTestKotlin {

  38.    kotlinOptions .jvmTarget = "1.8"

  39. }

举个例子:通过FreeMarker渲染一个页面。

  1. import org.springframework.stereotype.Controller

  2. import org.springframework.ui.Model

  3. import org.springframework.web.bind.annotation.RequestMapping

  4. /**

  5. * Created by http://quanke.name on 2018/1/10.

  6. */

  7. @Controller

  8. class HelloController {

  9.    @RequestMapping("/")

  10.    fun index(model: Model): String {

  11. //        / 加入一个属性,用来在模板中读取

  12.        model.addAttribute("host", "http://quanke.name")

  13.        // return模板文件的名称,对应src/main/resources/templates/index.ftl

  14.        return "index"

  15.    }

  16. }

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

注意Freemarker模版的后缀默认是 ftl,Thymeleaf模版后缀默认是  html

  1. <!DOCTYPE html>

  2. <html>

  3. <body>

  4. <h4>我的博客:${host}!</h4>

  5. <h1>quanke.name</h1>

  6. </body>

  7. </html>

增加使用 kotlin语言实现的  Spring Boot启动方法:

                                        
  1. import org.springframework .boot. SpringApplication

  2. import org.springframework .boot. autoconfigure.SpringBootApplication

  3. /**

  4. * Created by http://quanke.name on 2018/1/9.

  5. */

  6. @SpringBootApplication

  7. class Application

  8. fun main (args: Array< String>) {

  9.     SpringApplication.run (Application:: class.java , * args)

  10. }

在 application.yml文中增加:

                                            
  1. #设定ftl文件路径

  2. spring :

  3.  freemarker :

  4.     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 实战》系列:

我的第一个Kotlin应用

使用Spring Boot和Kotlin创建RESTfull API

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


全科龙婷▼升职加薪