Top Description Fields Constructors Methods
org.springframework.web.reactive.result.view.script

public Class ScriptTemplateConfigurer

extends Object
implements ScriptTemplateConfig
Class Inheritance
All Implemented Interfaces
org.springframework.web.reactive.result.view.script.ScriptTemplateConfig
Imports
java.nio.charset.Charset, java.util.function.Supplier, javax.script.Bindings, .ScriptEngine, org.springframework.lang.Nullable

An implementation of the Spring WebFlux ScriptTemplateConfig for creating a ScriptEngine for use in a web application.
// Add the following to an @Configuration class
@Bean
public ScriptTemplateConfigurer mustacheConfigurer() {
   ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
   configurer.setEngineName("nashorn");
   configurer.setScripts("mustache.js");
   configurer.setRenderObject("Mustache");
   configurer.setRenderFunction("render");
   return configurer;
}

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 sharedEngine property to false.

Author
Sebastien Deleuze
Since
5.0
See Also
ScriptTemplateView

Field Summary

Modifier and TypeField and Description
private Charset
private ScriptEngine
private String
private Supplier<ScriptEngine>
private String
private String
private String
private String[]
private Boolean

Constructor Summary

AccessConstructor and Description
public
ScriptTemplateConfigurer()

Default constructor.

public
ScriptTemplateConfigurer(String engineName)

Create a new ScriptTemplateConfigurer using the given engine name.

Method Summary

Modifier and TypeMethod and Description
public Charset
getCharset()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getCharset.

Return the charset used to read script and template files.

public ScriptEngine
public String
getEngineName()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngineName.

Return the engine name that will be used to instantiate the ScriptEngine.

public Supplier<ScriptEngine>
getEngineSupplier()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngineSupplier.

Return the engine supplier that will be used to instantiate the ScriptEngine.

public String
public String
getRenderObject()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getRenderObject.

Return the object where the render function belongs (optional).

public String
getResourceLoaderPath()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getResourceLoaderPath.

Return the resource loader path(s) via a Spring resource location.

public String[]
getScripts()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getScripts.

Return the scripts to be loaded by the script engine (library or user provided).

public Boolean
isSharedEngine()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.isSharedEngine.

Return whether to use a shared engine for all threads or whether to create thread-local engine instances for each thread.

public void
setCharset(Charset charset)

Set the charset used to read script and template files.

public void
setEngine(ScriptEngine engine)

Set the ScriptEngine to use by the view.

public void
setEngineName(String engineName)

Set the engine name that will be used to instantiate the ScriptEngine.

public void
setEngineSupplier(Supplier<ScriptEngine> engineSupplier)

Set the ScriptEngine supplier to use by the view, usually used with setSharedEngine(Boolean) set to false.

public void
setRenderFunction(String renderFunction)

Set the render function name (optional).

public void
setRenderObject(String renderObject)

Set the object where the render function belongs (optional).

public void
setResourceLoaderPath(String resourceLoaderPath)

Set the resource loader path(s) via a Spring resource location.

public void
setScripts(String... scriptNames)

Set the scripts to be loaded by the script engine (library or user provided).

public void
setSharedEngine(Boolean sharedEngine)

When set to false, a new ScriptEngine instance will be created for each request, else the same instance will be reused.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

charsetback to summary
private Charset charset
Annotations
@Nullable
engineback to summary
private ScriptEngine engine
Annotations
@Nullable
engineNameback to summary
private String engineName
Annotations
@Nullable
engineSupplierback to summary
private Supplier<ScriptEngine> engineSupplier
Annotations
@Nullable
renderFunctionback to summary
private String renderFunction
Annotations
@Nullable
renderObjectback to summary
private String renderObject
Annotations
@Nullable
resourceLoaderPathback to summary
private String resourceLoaderPath
Annotations
@Nullable
scriptsback to summary
private String[] scripts
Annotations
@Nullable
sharedEngineback to summary
private Boolean sharedEngine
Annotations
@Nullable

Constructor Detail

ScriptTemplateConfigurerback to summary
public ScriptTemplateConfigurer()

Default constructor.

ScriptTemplateConfigurerback to summary
public ScriptTemplateConfigurer(String engineName)

Create a new ScriptTemplateConfigurer using the given engine name.

Method Detail

getCharsetback to summary
public Charset getCharset()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getCharset.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getCharset.

Return the charset used to read script and template files.

Annotations
@Override
@Nullable
getEngineback to summary
public ScriptEngine getEngine()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngine.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngine.

Return the ScriptEngine to use by the views.

Annotations
@Override
@Nullable
getEngineNameback to summary
public String getEngineName()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngineName.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngineName.

Return the engine name that will be used to instantiate the ScriptEngine.

Annotations
@Override
@Nullable
getEngineSupplierback to summary
public Supplier<ScriptEngine> getEngineSupplier()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngineSupplier.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getEngineSupplier.

Return the engine supplier that will be used to instantiate the ScriptEngine.

Annotations
@Override
@Nullable
getRenderFunctionback to summary
public String getRenderFunction()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getRenderFunction.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getRenderFunction.

Return the render function name (optional). If not specified, the script templates will be evaluated with ScriptEngine#eval(String, Bindings).

Annotations
@Override
@Nullable
getRenderObjectback to summary
public String getRenderObject()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getRenderObject.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getRenderObject.

Return the object where the render function belongs (optional).

Annotations
@Override
@Nullable
getResourceLoaderPathback to summary
public String getResourceLoaderPath()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getResourceLoaderPath.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getResourceLoaderPath.

Return the resource loader path(s) via a Spring resource location.

Annotations
@Override
@Nullable
getScriptsback to summary
public String[] getScripts()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getScripts.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.getScripts.

Return the scripts to be loaded by the script engine (library or user provided).

Annotations
@Override
@Nullable
isSharedEngineback to summary
public Boolean isSharedEngine()

Implements org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.isSharedEngine.

Doc from org.springframework.web.reactive.result.view.script.ScriptTemplateConfig.isSharedEngine.

Return whether to use a shared engine for all threads or whether to create thread-local engine instances for each thread.

Annotations
@Override
@Nullable
setCharsetback to summary
public void setCharset(Charset charset)

Set the charset used to read script and template files. (UTF-8 by default).

setEngineback to summary
public void setEngine(ScriptEngine engine)

Set the ScriptEngine to use by the view. If renderFunction is specified, the script engine must implement Invocable. You must define engine or engineName, not both.

When the sharedEngine flag is set to false, you should not specify the script engine with this setter, but with the setEngineName(String) or setEngineSupplier(Supplier) (since it implies multiple lazy instantiations of the script engine).

See Also
setEngineName(String), setEngineSupplier(Supplier)
setEngineNameback to summary
public void setEngineName(String engineName)

Set the engine name that will be used to instantiate the ScriptEngine. If renderFunction is specified, the script engine must implement Invocable. You must define engine or engineName, not both.

See Also
setEngine(ScriptEngine), setEngineSupplier(Supplier)
setEngineSupplierback to summary
public void setEngineSupplier(Supplier<ScriptEngine> engineSupplier)

Set the ScriptEngine supplier to use by the view, usually used with setSharedEngine(Boolean) set to false. If renderFunction is specified, the script engine must implement Invocable. You must either define engineSupplier, engine or engineName.

Since
5.2
See Also
setEngine(ScriptEngine), setEngineName(String)
setRenderFunctionback to summary
public void setRenderFunction(String renderFunction)

Set the render function name (optional). If not specified, the script templates will be evaluated with ScriptEngine#eval(String, Bindings).

This function will be called with the following parameters:

  1. String template: the template content
  2. Map model: the view model
  3. RenderingContext context: the rendering context
See Also
RenderingContext
setRenderObjectback to summary
public void setRenderObject(String renderObject)

Set the object where the render function belongs (optional). For example, in order to call Mustache.render(), renderObject should be set to "Mustache" and renderFunction to "render".

setResourceLoaderPathback to summary
public void setResourceLoaderPath(String resourceLoaderPath)

Set the resource loader path(s) via a Spring resource location. Accepts multiple locations as a comma-separated list of paths. Standard URLs like "file:" and "classpath:" and pseudo URLs are supported as understood by Spring's org.springframework.core.io.ResourceLoader. Relative paths are allowed when running in an ApplicationContext.

Default is "classpath:".

setScriptsback to summary
public void setScripts(String... scriptNames)

Set the scripts to be loaded by the script engine (library or user provided). Since resourceLoaderPath default value is "classpath:", you can load easily any script available on the classpath.

For example, in order to use a JavaScript library available as a WebJars dependency and a custom "render.js" file, you should call configurer.setScripts("/META-INF/resources/webjars/library/version/library.js", "com/myproject/script/render.js");.

See Also
setResourceLoaderPath, WebJars
setSharedEngineback to summary
public void setSharedEngine(Boolean sharedEngine)

When set to false, a new ScriptEngine instance will be created for each request, else the same instance will be reused. This flag should be set to 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.

When this flag is set to false, the script engine must be specified using setEngineName(String). Using setEngine(ScriptEngine) is not possible because multiple instances of the script engine need to be created for each request.

See Also
THREADING ScriptEngine parameter