Class ScriptTemplateConfigurer
- All Implemented Interfaces:
ScriptTemplateConfig
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
.
- Since:
- 4.2
- Author:
- Sebastien Deleuze
- See Also:
-
Constructor Summary
ConstructorDescriptionDefault constructor.ScriptTemplateConfigurer
(String engineName) Create a new ScriptTemplateConfigurer using the given engine name. -
Method Summary
Modifier and TypeMethodDescriptionReturn the charset used to read script and template files.Return the content type to use for the response.Return theScriptEngine
to use by the views.Return the engine name that will be used to instantiate theScriptEngine
.Return the engine supplier that will be used to instantiate theScriptEngine
.Return the render function name (optional).Return the object where the render function belongs (optional).Return the resource loader path(s) via a Spring resource location.String[]
Return the scripts to be loaded by the script engine (library or user provided).Return whether to use a shared engine for all threads or whether to create thread-local engine instances for each thread.void
setCharset
(Charset charset) Set the charset used to read script and template files.void
setContentType
(String contentType) Set the content type to use for the response.void
setEngine
(ScriptEngine engine) Set theScriptEngine
to use by the view.void
setEngineName
(String engineName) Set the engine name that will be used to instantiate theScriptEngine
.void
setEngineSupplier
(Supplier<ScriptEngine> engineSupplier) Set theScriptEngine
supplier to use by the view, usually used withsetSharedEngine(Boolean)
set tofalse
.void
setRenderFunction
(String renderFunction) Set the render function name (optional).void
setRenderObject
(String renderObject) Set the object where the render function belongs (optional).void
setResourceLoaderPath
(String resourceLoaderPath) Set the resource loader path(s) via a Spring resource location.void
setScripts
(String... scriptNames) Set the scripts to be loaded by the script engine (library or user provided).void
setSharedEngine
(Boolean sharedEngine) When set tofalse
, use thread-localScriptEngine
instances instead of one single shared instance.
-
Constructor Details
-
ScriptTemplateConfigurer
public ScriptTemplateConfigurer()Default constructor. -
ScriptTemplateConfigurer
Create a new ScriptTemplateConfigurer using the given engine name.
-
-
Method Details
-
setEngine
Set theScriptEngine
to use by the view. IfrenderFunction
is specified, the script engine must implementInvocable
. You must defineengine
orengineName
, not both.When the
sharedEngine
flag is set tofalse
, you should not specify the script engine with this setter, but withsetEngineName(String)
orsetEngineSupplier(Supplier)
since it implies multiple lazy instantiations of the script engine. -
getEngine
Description copied from interface:ScriptTemplateConfig
Return theScriptEngine
to use by the views.- Specified by:
getEngine
in interfaceScriptTemplateConfig
-
setEngineSupplier
Set theScriptEngine
supplier to use by the view, usually used withsetSharedEngine(Boolean)
set tofalse
. IfrenderFunction
is specified, the script engine must implementInvocable
. You must either defineengineSupplier
,engine
orengineName
.- Since:
- 5.2
- See Also:
-
getEngineSupplier
Description copied from interface:ScriptTemplateConfig
Return the engine supplier that will be used to instantiate theScriptEngine
.- Specified by:
getEngineSupplier
in interfaceScriptTemplateConfig
-
setEngineName
Set the engine name that will be used to instantiate theScriptEngine
. IfrenderFunction
is specified, the script engine must implementInvocable
. You must defineengine
orengineName
, not both. -
getEngineName
Description copied from interface:ScriptTemplateConfig
Return the engine name that will be used to instantiate theScriptEngine
.- Specified by:
getEngineName
in interfaceScriptTemplateConfig
-
setScripts
Set the scripts to be loaded by the script engine (library or user provided). SinceresourceLoaderPath
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");
. -
getScripts
Description copied from interface:ScriptTemplateConfig
Return the scripts to be loaded by the script engine (library or user provided).- Specified by:
getScripts
in interfaceScriptTemplateConfig
-
setRenderObject
Set the object where the render function belongs (optional). For example, in order to callMustache.render()
,renderObject
should be set to"Mustache"
andrenderFunction
to"render"
. -
getRenderObject
Description copied from interface:ScriptTemplateConfig
Return the object where the render function belongs (optional).- Specified by:
getRenderObject
in interfaceScriptTemplateConfig
-
setRenderFunction
Set the render function name (optional). If not specified, the script templates will be evaluated withScriptEngine.eval(String, Bindings)
.This function will be called with the following parameters:
String template
: the template contentMap model
: the view modelRenderingContext context
: the rendering context (since 5.0)
- See Also:
-
getRenderFunction
Description copied from interface:ScriptTemplateConfig
Return the render function name (optional). If not specified, the script templates will be evaluated withScriptEngine.eval(String, Bindings)
.- Specified by:
getRenderFunction
in interfaceScriptTemplateConfig
-
setContentType
Set the content type to use for the response. (text/html
by default).- Since:
- 4.2.1
-
getContentType
Return the content type to use for the response.- Specified by:
getContentType
in interfaceScriptTemplateConfig
- Since:
- 4.2.1
-
setCharset
Set the charset used to read script and template files. (UTF-8
by default). -
getCharset
Description copied from interface:ScriptTemplateConfig
Return the charset used to read script and template files.- Specified by:
getCharset
in interfaceScriptTemplateConfig
-
setResourceLoaderPath
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'sResourceLoader
. Relative paths are allowed when running in an ApplicationContext.Default is "classpath:".
-
getResourceLoaderPath
Description copied from interface:ScriptTemplateConfig
Return the resource loader path(s) via a Spring resource location.- Specified by:
getResourceLoaderPath
in interfaceScriptTemplateConfig
-