public class ScriptTemplateConfigurer extends Object implements ScriptTemplateConfig, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.InitializingBean
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;
}
ScriptTemplateView| Constructor and Description |
|---|
ScriptTemplateConfigurer() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterPropertiesSet() |
protected ClassLoader |
createClassLoader() |
protected ScriptEngine |
createScriptEngine() |
protected org.springframework.context.ApplicationContext |
getApplicationContext() |
Charset |
getCharset() |
ScriptEngine |
getEngine() |
String |
getRenderFunction() |
String |
getRenderObject() |
org.springframework.core.io.ResourceLoader |
getResourceLoader() |
String |
getResourceLoaderPath() |
void |
setApplicationContext(org.springframework.context.ApplicationContext applicationContext) |
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 |
setRenderFunction(String renderFunction)
Set the render function name (mandatory).
|
void |
setRenderObject(String renderObject)
Set the object where belongs the render function (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).
|
public void setEngine(ScriptEngine engine)
ScriptEngine to use by the view.
The script engine must implement Invocable.
You must define engine or engineName, not both.public ScriptEngine getEngine()
getEngine in interface ScriptTemplateConfigpublic void setEngineName(String engineName)
ScriptEngine.
The script engine must implement Invocable.
You must define engine or engineName, not both.public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
setApplicationContext in interface org.springframework.context.ApplicationContextAwareprotected org.springframework.context.ApplicationContext getApplicationContext()
public void setScripts(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(String),
WebJarspublic String getRenderObject()
getRenderObject in interface ScriptTemplateConfigpublic void setRenderObject(String renderObject)
Mustache.render(), renderObject
should be set to "Mustache" and renderFunction to "render".public String getRenderFunction()
getRenderFunction in interface ScriptTemplateConfigpublic void setRenderFunction(String renderFunction)
template: the view template content (String)model: the view model (Map)public void setCharset(Charset charset)
UTF-8 by default).public Charset getCharset()
getCharset in interface ScriptTemplateConfigpublic void setResourceLoaderPath(String resourceLoaderPath)
ResourceLoader.
Relative paths are allowed when running in an ApplicationContext.
Default is "classpath:".public String getResourceLoaderPath()
public org.springframework.core.io.ResourceLoader getResourceLoader()
getResourceLoader in interface ScriptTemplateConfigpublic void afterPropertiesSet()
throws Exception
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBeanExceptionprotected ClassLoader createClassLoader() throws IOException
IOExceptionprotected ScriptEngine createScriptEngine() throws IOException
IOException