public class ScriptTemplateConfigurer extends Object implements 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.
ScriptTemplateView| Constructor and Description |
|---|
ScriptTemplateConfigurer()
Default constructor.
|
ScriptTemplateConfigurer(String engineName)
Create a new ScriptTemplateConfigurer using the given engine name.
|
| Modifier and Type | Method and Description |
|---|---|
Charset |
getCharset()
Return the charset used to read script and template files.
|
ScriptEngine |
getEngine()
Return the
ScriptEngine to use by the views. |
String |
getEngineName()
Return the engine name that will be used to instantiate the
ScriptEngine. |
Supplier<ScriptEngine> |
getEngineSupplier()
Return the engine supplier that will be used to instantiate the
ScriptEngine. |
String |
getRenderFunction()
Return the render function name (optional).
|
String |
getRenderObject()
Return the object where the render function belongs (optional).
|
String |
getResourceLoaderPath()
Return the resource loader path(s) via a Spring resource location.
|
String[] |
getScripts()
Return the scripts to be loaded by the script engine (library or user provided).
|
Boolean |
isSharedEngine()
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 |
setEngine(ScriptEngine engine)
Set the
ScriptEngine to use by the view. |
void |
setEngineName(String engineName)
Set the engine name that will be used to instantiate the
ScriptEngine. |
void |
setEngineSupplier(Supplier<ScriptEngine> engineSupplier)
Set the
ScriptEngine supplier to use by the view, usually used with
setSharedEngine(Boolean) set to false. |
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 to
false, a new ScriptEngine instance will be created
for each request, else the same instance will be reused. |
public ScriptTemplateConfigurer()
public ScriptTemplateConfigurer(String engineName)
public void setEngine(@Nullable ScriptEngine engine)
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).
setEngineName(String),
setEngineSupplier(Supplier)@Nullable public ScriptEngine getEngine()
ScriptTemplateConfigScriptEngine to use by the views.getEngine in interface ScriptTemplateConfigpublic void setEngineSupplier(@Nullable Supplier<ScriptEngine> engineSupplier)
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.setEngine(ScriptEngine),
setEngineName(String)@Nullable public Supplier<ScriptEngine> getEngineSupplier()
ScriptTemplateConfigScriptEngine.getEngineSupplier in interface ScriptTemplateConfigpublic void setEngineName(@Nullable String engineName)
ScriptEngine.
If renderFunction is specified, the script engine must implement Invocable.
You must define engine or engineName, not both.@Nullable public String getEngineName()
ScriptTemplateConfigScriptEngine.getEngineName in interface ScriptTemplateConfigpublic void setSharedEngine(@Nullable Boolean sharedEngine)
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.
@Nullable public Boolean isSharedEngine()
ScriptTemplateConfigisSharedEngine in interface ScriptTemplateConfigpublic void setScripts(@Nullable String... scriptNames)
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");.
setResourceLoaderPath(java.lang.String),
WebJars@Nullable public String[] getScripts()
ScriptTemplateConfiggetScripts in interface ScriptTemplateConfigpublic void setRenderObject(@Nullable String renderObject)
Mustache.render(), renderObject
should be set to "Mustache" and renderFunction to "render".@Nullable public String getRenderObject()
ScriptTemplateConfiggetRenderObject in interface ScriptTemplateConfigpublic void setRenderFunction(@Nullable String renderFunction)
ScriptEngine.eval(String, Bindings).
This function will be called with the following parameters:
String template: the template contentMap model: the view modelRenderingContext context: the rendering contextRenderingContext@Nullable public String getRenderFunction()
ScriptTemplateConfigScriptEngine.eval(String, Bindings).getRenderFunction in interface ScriptTemplateConfigpublic void setCharset(@Nullable Charset charset)
UTF-8 by default).@Nullable public Charset getCharset()
ScriptTemplateConfiggetCharset in interface ScriptTemplateConfigpublic void setResourceLoaderPath(@Nullable String resourceLoaderPath)
ResourceLoader.
Relative paths are allowed when running in an ApplicationContext.
Default is "classpath:".
@Nullable public String getResourceLoaderPath()
ScriptTemplateConfiggetResourceLoaderPath in interface ScriptTemplateConfig