{{body}}
- - ----- - -The render function is called with the following parameters: - -* `String template`: The template content -* `Map model`: The view model -* `RenderingContext renderingContext`: The - {spring-framework-api}/web/servlet/view/script/RenderingContext.html[`RenderingContext`] - that gives access to the application context, the locale, the template loader, and the - URL (since 5.0) - -`Mustache.render()` is natively compatible with this signature, so you can call it directly. - -If your templating technology requires some customization, you can provide a script that -implements a custom render function. For example, https://handlebarsjs.com[Handlerbars] -needs to compile templates before using them and requires a -https://en.wikipedia.org/wiki/Polyfill[polyfill] to emulate some -browser facilities that are not available in the server-side script engine. - -The following example shows how to do so: - -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableWebMvc - public class WebConfig implements WebMvcConfigurer { - - @Override - public void configureViewResolvers(ViewResolverRegistry registry) { - registry.scriptTemplate(); - } - - @Bean - public ScriptTemplateConfigurer configurer() { - ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer(); - configurer.setEngineName("nashorn"); - configurer.setScripts("polyfill.js", "handlebars.js", "render.js"); - configurer.setRenderFunction("render"); - configurer.setSharedEngine(false); - return configurer; - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableWebMvc - class WebConfig : WebMvcConfigurer { - - override fun configureViewResolvers(registry: ViewResolverRegistry) { - registry.scriptTemplate() - } - - @Bean - fun configurer() = ScriptTemplateConfigurer().apply { - engineName = "nashorn" - setScripts("polyfill.js", "handlebars.js", "render.js") - renderFunction = "render" - isSharedEngine = false - } - } ----- -====== - -NOTE: Setting the `sharedEngine` property to `false` is required when using non-thread-safe -script engines with templating libraries not designed for concurrency, such as Handlebars or -React running on Nashorn. In that case, Java SE 8 update 60 is required, due to -https://bugs.openjdk.java.net/browse/JDK-8076099[this bug], but it is generally -recommended to use a recent Java SE patch release in any case. - -`polyfill.js` defines only the `window` object needed by Handlebars to run properly, as follows: - -[source,javascript,indent=0,subs="verbatim,quotes"] ----- - var window = {}; ----- - -This basic `render.js` implementation compiles the template before using it. A production-ready -implementation should also store any reused cached templates or pre-compiled templates. -You can do so on the script side (and handle any customization you need -- managing -template engine configuration, for example). The following example shows how to do so: - -[source,javascript,indent=0,subs="verbatim,quotes"] ----- - function render(template, model) { - var compiledTemplate = Handlebars.compile(template); - return compiledTemplate(model); - } ----- - Check out the Spring Framework unit tests, {spring-framework-code}/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script[Java], and {spring-framework-code}/spring-webmvc/src/test/resources/org/springframework/web/servlet/view/script[resources], diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateConfigurer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateConfigurer.java index 78c9c8e06c3..c5076570dda 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateConfigurer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateConfigurer.java @@ -42,8 +42,8 @@ import org.jspecify.annotations.Nullable; * * *NOTE: It is possible to use non thread-safe script engines with - * templating libraries not designed for concurrency, like Handlebars or React running on - * Nashorn, by setting the {@link #setSharedEngine sharedEngine} property to {@code false}. + * templating libraries not designed for concurrency by setting the + * {@link #setSharedEngine sharedEngine} property to {@code false}. * * @author Sebastien Deleuze * @since 5.0 @@ -143,7 +143,7 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig { * for each request, else the same instance will be reused. * This flag should be set to {@code false} for those using non thread-safe script * engines with templating libraries not designed for - * concurrency, like Handlebars or React running on Nashorn for example. + * concurrency. *
When this flag is set to {@code false}, the script engine must be specified using * {@link #setEngineName(String)}. Using {@link #setEngine(ScriptEngine)} is not * possible because multiple instances of the script engine need to be created for diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateView.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateView.java index 083d3e4b4de..b9e298db838 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateView.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateView.java @@ -58,10 +58,6 @@ import org.springframework.web.server.ServerWebExchange; * {@link ScriptTemplateConfig} bean in the web application context and using * it to obtain the configured properties. * - *
The Nashorn JavaScript engine requires Java 8+ and may require setting the - * {@code sharedEngine} property to {@code false} in order to run properly. See - * {@link ScriptTemplateConfigurer#setSharedEngine(Boolean)} for more details. - * * @author Sebastien Deleuze * @author Juergen Hoeller * @since 5.0 diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateConfigurer.java index 6d8c0019e56..f47bcd00e68 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateConfigurer.java @@ -42,8 +42,8 @@ import org.jspecify.annotations.Nullable; * * *
NOTE: It is possible to use non thread-safe script engines with - * templating libraries not designed for concurrency, like Handlebars or React running on - * Nashorn, by setting the {@link #setSharedEngine sharedEngine} property to {@code false}. + * templating libraries not designed for concurrency by setting the + * {@link #setSharedEngine sharedEngine} property to {@code false}. * * @author Sebastien Deleuze * @since 4.2 @@ -144,7 +144,7 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig { * When set to {@code false}, use thread-local {@link ScriptEngine} instances instead * of one single shared instance. This flag should be set to {@code false} for those * using non thread-safe script engines with templating libraries not designed for - * concurrency, like Handlebars or React running on Nashorn for example. + * concurrency. *
When this flag is set to {@code false}, the script engine must be specified using * {@link #setEngineName(String)} or {@link #setEngineSupplier(Supplier)}. * Using {@link #setEngine(ScriptEngine)} is not possible because multiple instances diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java index 0ee1774e1d0..3f6cc6344b7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java @@ -62,10 +62,6 @@ import org.springframework.web.servlet.view.AbstractUrlBasedView; * {@link ScriptTemplateConfig} bean in the web application context and using * it to obtain the configured properties. * - *
The Nashorn JavaScript engine requires Java 8+ and may require setting the
- * {@code sharedEngine} property to {@code false} in order to run properly. See
- * {@link ScriptTemplateConfigurer#setSharedEngine(Boolean)} for more details.
- *
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 4.2
diff --git a/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc.xsd b/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc.xsd
index dcb56f5cc31..7b8da7ba4fc 100644
--- a/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc.xsd
+++ b/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc.xsd
@@ -1201,9 +1201,7 @@