Skip to content

Commit e86b937

Browse files
committed
Leverage new Kotlin script templating support
As of 1.3.40, Kotlin now provides a kotlin-scripting-jsr223-embeddable dependency which: - Fixes classloading related issues - Provides out of the box JSR 223 support - Is compatible with Spring Boot Fat Jar mechanism This commit updates Spring Framework tests and documentation accordingly. Closes gh-23165
1 parent cfb4148 commit e86b937

File tree

7 files changed

+36
-38
lines changed

7 files changed

+36
-38
lines changed

spring-webflux/spring-webflux.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ dependencies {
5555
testCompile("org.eclipse.jetty:jetty-servlet")
5656
testCompile("org.eclipse.jetty:jetty-reactive-httpclient:1.0.3")
5757
testCompile("com.squareup.okhttp3:mockwebserver:3.14.2")
58-
testCompile("org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}")
5958
testCompile(project(":spring-core-coroutines"))
60-
testRuntime("org.jetbrains.kotlin:kotlin-script-util:${kotlinVersion}")
61-
testRuntime("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
59+
testCompile("org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}")
60+
testRuntime("org.jetbrains.kotlin:kotlin-scripting-jsr223-embeddable:${kotlinVersion}")
6261
testRuntime("org.jruby:jruby:9.2.7.0")
6362
testRuntime("org.python:jython-standalone:2.7.1")
6463
testRuntime("org.synchronoss.cloud:nio-multipart-parser:1.1.0")

spring-webflux/src/test/java/org/springframework/web/reactive/result/view/script/KotlinScriptTemplateTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Locale;
2121
import java.util.Map;
2222

23-
import org.junit.Ignore;
2423
import org.junit.Test;
2524

2625
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -41,7 +40,6 @@
4140
*/
4241
public class KotlinScriptTemplateTests {
4342

44-
@Ignore
4543
@Test
4644
public void renderTemplateWithFrenchLocale() throws Exception {
4745
Map<String, Object> model = new HashMap<>();
@@ -51,7 +49,6 @@ public void renderTemplateWithFrenchLocale() throws Exception {
5149
assertThat(response.getBodyAsString().block()).isEqualTo("<html><body>\n<p>Bonjour Foo</p>\n</body></html>");
5250
}
5351

54-
@Ignore
5552
@Test
5653
public void renderTemplateWithEnglishLocale() throws Exception {
5754
Map<String, Object> model = new HashMap<>();
@@ -61,7 +58,6 @@ public void renderTemplateWithEnglishLocale() throws Exception {
6158
assertThat(response.getBodyAsString().block()).isEqualTo("<html><body>\n<p>Hello Foo</p>\n</body></html>");
6259
}
6360

64-
@Ignore
6561
@Test
6662
public void renderTemplateWithoutRenderFunction() throws Exception {
6763
Map<String, Object> model = new HashMap<>();

spring-webflux/src/test/resources/META-INF/services/javax.script.ScriptEngineFactory

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-webmvc/spring-webmvc.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ dependencies {
7373
testCompile("io.reactivex:rxjava-reactive-streams:${rxjavaAdapterVersion}")
7474
testCompile("io.reactivex.rxjava2:rxjava:${rxjava2Version}")
7575
testCompile("org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}")
76-
testRuntime("org.jetbrains.kotlin:kotlin-script-util:${kotlinVersion}")
77-
testRuntime("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
76+
testRuntime("org.jetbrains.kotlin:kotlin-scripting-jsr223-embeddable:${kotlinVersion}")
7877
testRuntime("org.jruby:jruby:9.2.7.0")
7978
testRuntime("org.python:jython-standalone:2.7.1")
8079
testRuntime("org.webjars:underscorejs:1.8.3")

spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/KotlinScriptTemplateTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import javax.servlet.ServletContext;
2323

2424
import org.junit.Before;
25-
import org.junit.Ignore;
2625
import org.junit.Test;
2726

2827
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -56,7 +55,6 @@ public void setup() {
5655
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
5756
}
5857

59-
@Ignore
6058
@Test
6159
public void renderTemplateWithFrenchLocale() throws Exception {
6260
Map<String, Object> model = new HashMap<>();
@@ -66,7 +64,6 @@ public void renderTemplateWithFrenchLocale() throws Exception {
6664
assertThat(response.getContentAsString()).isEqualTo("<html><body>\n<p>Bonjour Foo</p>\n</body></html>");
6765
}
6866

69-
@Ignore
7067
@Test
7168
public void renderTemplateWithEnglishLocale() throws Exception {
7269
Map<String, Object> model = new HashMap<>();
@@ -76,7 +73,6 @@ public void renderTemplateWithEnglishLocale() throws Exception {
7673
assertThat(response.getContentAsString()).isEqualTo("<html><body>\n<p>Hello Foo</p>\n</body></html>");
7774
}
7875

79-
@Ignore
8076
@Test
8177
public void renderTemplateWithoutRenderFunction() throws Exception {
8278
Map<String, Object> model = new HashMap<>();

spring-webmvc/src/test/resources/META-INF/services/javax.script.ScriptEngineFactory

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/docs/asciidoc/languages/kotlin.adoc

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -337,39 +337,49 @@ mockMvc.get("/person/{name}", "Lee") {
337337

338338
=== Kotlin Script Templates
339339

340-
As of version 4.3, Spring Framework provides a
340+
Spring Framework provides a
341341
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/view/script/ScriptTemplateView.html[`ScriptTemplateView`]
342-
to render templates by using script engines. It supports
343-
https://www.jcp.org/en/jsr/detail?id=223[JSR-223].
344-
Spring Framework 5 goes even further by extending this feature to WebFlux and supporting
345-
https://jira.spring.io/browse/SPR-15064[i18n and nested templates].
342+
which supports https://www.jcp.org/en/jsr/detail?id=223[JSR-223] to render templates by using script engines.
346343

347-
Kotlin provides similar support and allows the rendering of Kotlin-based templates. See
348-
https://github.com/spring-projects/spring-framework/commit/badde3a479a53e1dd0777dd1bd5b55cb1021cf9e[this commit] for details.
344+
By leveraging `kotlin-script-runtime` and `scripting-jsr223-embeddable` dependencies, it
345+
is possible to use such feature to render Kotlin-based templates with
346+
https://github.com/Kotlin/kotlinx.html[kotlinx.html] DSL or Kotlin multiline interpolated `String`.
349347

350-
This enables some interesting use cases - such as writing type-safe templates by using
351-
https://github.com/Kotlin/kotlinx.html[kotlinx.html] DSL or by a using Kotlin multiline `String` with interpolation.
348+
`build.gradle.kts`
349+
[source,kotlin,indent=0]
350+
----
351+
dependencies {
352+
compile("org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}")
353+
runtime("org.jetbrains.kotlin:kotlin-scripting-jsr223-embeddable:${kotlinVersion}")
354+
}
355+
----
352356

353-
This can let you write Kotlin templates with full autocompletion and
354-
refactoring support in a supported IDE, as the following example shows:
357+
Configuration is usually done with `ScriptTemplateConfigurer` and `ScriptTemplateViewResolver`
358+
beans.
355359

360+
361+
`KotlinScriptConfiguration.kt`
356362
[source,kotlin,indent=0]
357363
----
358-
import io.spring.demo.*
364+
@Configuration
365+
class KotlinScriptConfiguration {
366+
367+
@Bean
368+
fun kotlinScriptConfigurer() = ScriptTemplateConfigurer().apply {
369+
engineName = "kotlin"
370+
setScripts("scripts/render.kts")
371+
renderFunction = "render"
372+
isSharedEngine = false
373+
}
359374
360-
"""
361-
${include("header")}
362-
<h1>${i18n("title")}</h1>
363-
<ul>
364-
${users.joinToLine{ "<li>${i18n("user")} ${it.firstname} ${it.lastname}</li>" }}
365-
</ul>
366-
${include("footer")}
367-
"""
375+
@Bean
376+
fun kotlinScriptViewResolver() = ScriptTemplateViewResolver().apply {
377+
setPrefix("templates/")
378+
setSuffix(".kts")
379+
}
380+
}
368381
----
369382

370-
WARNING: Kotlin Script Templates support is experimental and not compatible yet with Spring Boot fatjar mechanism, see related
371-
https://youtrack.jetbrains.com/issue/KT-21443[KT-21443] and https://youtrack.jetbrains.com/issue/KT-27956[KT-27956]
372-
issues.
373383

374384
See the https://github.com/sdeleuze/kotlin-script-templating[kotlin-script-templating] example
375385
project for more details.

0 commit comments

Comments
 (0)