public class ScriptTemplateConfigurer extends java.lang.Object implements ScriptTemplateConfig, ApplicationContextAware, 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
Modifier and Type | Field and Description |
---|---|
private ApplicationContext |
applicationContext |
private java.nio.charset.Charset |
charset |
private javax.script.ScriptEngine |
engine |
private java.lang.String |
engineName |
private java.lang.String |
renderFunction |
private java.lang.String |
renderObject |
private ResourceLoader |
resourceLoader |
private java.lang.String |
resourceLoaderPath |
private java.lang.String[] |
scripts |
Constructor and Description |
---|
ScriptTemplateConfigurer() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected java.lang.ClassLoader |
createClassLoader() |
protected javax.script.ScriptEngine |
createScriptEngine() |
protected ApplicationContext |
getApplicationContext() |
java.nio.charset.Charset |
getCharset() |
javax.script.ScriptEngine |
getEngine() |
java.lang.String |
getRenderFunction() |
java.lang.String |
getRenderObject() |
ResourceLoader |
getResourceLoader() |
java.lang.String |
getResourceLoaderPath() |
private java.io.Reader |
read(java.lang.String path) |
void |
setApplicationContext(ApplicationContext applicationContext)
Set the ApplicationContext that this object runs in.
|
void |
setCharset(java.nio.charset.Charset charset)
Set the charset used to read script and template files.
|
void |
setEngine(javax.script.ScriptEngine engine)
Set the
ScriptEngine to use by the view. |
void |
setEngineName(java.lang.String engineName)
Set the engine name that will be used to instantiate the
ScriptEngine . |
void |
setRenderFunction(java.lang.String renderFunction)
Set the render function name (mandatory).
|
void |
setRenderObject(java.lang.String renderObject)
Set the object where belongs the render function (optional).
|
void |
setResourceLoaderPath(java.lang.String resourceLoaderPath)
Set the resource loader path(s) via a Spring resource location.
|
void |
setScripts(java.lang.String... scriptNames)
Set the scripts to be loaded by the script engine (library or user provided).
|
private javax.script.ScriptEngine engine
private java.lang.String engineName
private ApplicationContext applicationContext
private java.lang.String[] scripts
private java.lang.String renderObject
private java.lang.String renderFunction
private java.nio.charset.Charset charset
private ResourceLoader resourceLoader
private java.lang.String resourceLoaderPath
public void setEngine(javax.script.ScriptEngine engine)
ScriptEngine
to use by the view.
The script engine must implement Invocable
.
You must define engine
or engineName
, not both.public javax.script.ScriptEngine getEngine()
getEngine
in interface ScriptTemplateConfig
public void setEngineName(java.lang.String engineName)
ScriptEngine
.
The script engine must implement Invocable
.
You must define engine
or engineName
, not both.public void setApplicationContext(ApplicationContext applicationContext)
ApplicationContextAware
Invoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and
MessageSourceAware
, if applicable.
setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectBeanInitializationException
protected ApplicationContext getApplicationContext()
public void setScripts(java.lang.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 java.lang.String getRenderObject()
getRenderObject
in interface ScriptTemplateConfig
public void setRenderObject(java.lang.String renderObject)
Mustache.render()
, renderObject
should be set to "Mustache"
and renderFunction
to "render"
.public java.lang.String getRenderFunction()
getRenderFunction
in interface ScriptTemplateConfig
public void setRenderFunction(java.lang.String renderFunction)
template
: the view template content (String)model
: the view model (Map)public void setCharset(java.nio.charset.Charset charset)
UTF-8
by default).public java.nio.charset.Charset getCharset()
getCharset
in interface ScriptTemplateConfig
public void setResourceLoaderPath(java.lang.String resourceLoaderPath)
ResourceLoader
.
Relative paths are allowed when running in an ApplicationContext.
Default is "classpath:".public java.lang.String getResourceLoaderPath()
public ResourceLoader getResourceLoader()
getResourceLoader
in interface ScriptTemplateConfig
public void afterPropertiesSet() throws java.lang.Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
java.lang.Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.protected java.lang.ClassLoader createClassLoader() throws java.io.IOException
java.io.IOException
private java.io.Reader read(java.lang.String path) throws java.io.IOException
java.io.IOException
protected javax.script.ScriptEngine createScriptEngine() throws java.io.IOException
java.io.IOException