Class ScriptTemplateConfigurer
- java.lang.Object
- org.springframework.web.servlet.view.script.ScriptTemplateConfigurer
- All Implemented Interfaces:
ScriptTemplateConfig
public class ScriptTemplateConfigurer extends Object implements ScriptTemplateConfig
An implementation of Spring MVC'sScriptTemplateConfigfor creating aScriptEnginefor 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
sharedEngineproperty tofalse.- Since:
- 4.2
- Author:
- Sebastien Deleuze
- See Also:
ScriptTemplateView
Constructor Summary
Constructors Constructor Description ScriptTemplateConfigurer()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CharsetgetCharset()Return the charset used to read script and template files.StringgetContentType()Return the content type to use for the response.ScriptEnginegetEngine()Return theScriptEngineto use by the views.StringgetEngineName()Return the engine name that will be used to instantiate theScriptEngine.StringgetRenderFunction()Return the render function name (mandatory).StringgetRenderObject()Return the object where the render function belongs (optional).StringgetResourceLoaderPath()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).BooleanisSharedEngine()Return whether to use a shared engine for all threads or whether to create thread-local engine instances for each thread.voidsetCharset(Charset charset)Set the charset used to read script and template files.voidsetContentType(String contentType)Set the content type to use for the response.voidsetEngine(ScriptEngine engine)Set theScriptEngineto use by the view.voidsetEngineName(String engineName)Set the engine name that will be used to instantiate theScriptEngine.voidsetRenderFunction(String renderFunction)Set the render function name (mandatory).voidsetRenderObject(String renderObject)Set the object where the render function belongs (optional).voidsetResourceLoaderPath(String resourceLoaderPath)Set the resource loader path(s) via a Spring resource location.voidsetScripts(String... scriptNames)Set the scripts to be loaded by the script engine (library or user provided).voidsetSharedEngine(Boolean sharedEngine)When set tofalse, use thread-localScriptEngineinstances instead of one single shared instance.
Constructor Detail
ScriptTemplateConfigurer
public ScriptTemplateConfigurer()
Method Detail
setEngine
public void setEngine(ScriptEngine engine)
Set theScriptEngineto use by the view. The script engine must implementInvocable. You must defineengineorengineName, not both.When the
sharedEngineflag is set tofalse, you should not specify the script engine with this setter, but with thesetEngineName(String)one (since it implies multiple lazy instantiations of the script engine).- See Also:
setEngineName(String)
getEngine
public ScriptEngine getEngine()
Description copied from interface:ScriptTemplateConfigReturn theScriptEngineto use by the views.- Specified by:
getEnginein interfaceScriptTemplateConfig
setEngineName
public void setEngineName(String engineName)
Set the engine name that will be used to instantiate theScriptEngine. The script engine must implementInvocable. You must defineengineorengineName, not both.- See Also:
setEngine(ScriptEngine)
getEngineName
public String getEngineName()
Description copied from interface:ScriptTemplateConfigReturn the engine name that will be used to instantiate theScriptEngine.- Specified by:
getEngineNamein interfaceScriptTemplateConfig
setSharedEngine
public void setSharedEngine(Boolean sharedEngine)
When set tofalse, use thread-localScriptEngineinstances instead of one single shared instance. This flag should be set tofalsefor those using non thread-safe script engines with templating libraries not designed for concurrency, like Handlebars or React running on Nashorn for example. In this case, Java 8u60 or greater is required due to this bug.When this flag is set to
false, the script engine must be specified usingsetEngineName(String). UsingsetEngine(ScriptEngine)is not possible because multiple instances of the script engine need to be created lazily (one per thread).- See Also:
- THREADING ScriptEngine parameter
isSharedEngine
public Boolean isSharedEngine()
Description copied from interface:ScriptTemplateConfigReturn whether to use a shared engine for all threads or whether to create thread-local engine instances for each thread.- Specified by:
isSharedEnginein interfaceScriptTemplateConfig
setScripts
public void setScripts(String... scriptNames)
Set the scripts to be loaded by the script engine (library or user provided). SinceresourceLoaderPathdefault 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
public String[] getScripts()
Description copied from interface:ScriptTemplateConfigReturn the scripts to be loaded by the script engine (library or user provided).- Specified by:
getScriptsin interfaceScriptTemplateConfig
setRenderObject
public void setRenderObject(String renderObject)
Set the object where the render function belongs (optional). For example, in order to callMustache.render(),renderObjectshould be set to"Mustache"andrenderFunctionto"render".
getRenderObject
public String getRenderObject()
Description copied from interface:ScriptTemplateConfigReturn the object where the render function belongs (optional).- Specified by:
getRenderObjectin interfaceScriptTemplateConfig
setRenderFunction
public void setRenderFunction(String renderFunction)
Set the render function name (mandatory).This function will be called with the following parameters:
String template: the template contentMap model: the view modelString url: the template url (since 4.2.2)
getRenderFunction
public String getRenderFunction()
Description copied from interface:ScriptTemplateConfigReturn the render function name (mandatory).- Specified by:
getRenderFunctionin interfaceScriptTemplateConfig
setContentType
public void setContentType(String contentType)
Set the content type to use for the response. (text/htmlby default).- Since:
- 4.2.1
getContentType
public String getContentType()
Return the content type to use for the response.- Specified by:
getContentTypein interfaceScriptTemplateConfig- Since:
- 4.2.1
setCharset
public void setCharset(Charset charset)
Set the charset used to read script and template files. (UTF-8by default).
getCharset
public Charset getCharset()
Description copied from interface:ScriptTemplateConfigReturn the charset used to read script and template files.- Specified by:
getCharsetin interfaceScriptTemplateConfig
setResourceLoaderPath
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'sResourceLoader. Relative paths are allowed when running in an ApplicationContext.Default is "classpath:".
getResourceLoaderPath
public String getResourceLoaderPath()
Description copied from interface:ScriptTemplateConfigReturn the resource loader path(s) via a Spring resource location.- Specified by:
getResourceLoaderPathin interfaceScriptTemplateConfig