类 ScriptTemplateConfigurer
- java.lang.Object
- org.springframework.web.servlet.view.script.ScriptTemplateConfigurer
- 所有已实现的接口:
ScriptTemplateConfig
public class ScriptTemplateConfigurer extends Object implements ScriptTemplateConfig
An implementation of Spring MVC'sScriptTemplateConfig
for creating aScriptEngine
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 tofalse
.- 从以下版本开始:
- 4.2
- 作者:
- Sebastien Deleuze
- 另请参阅:
ScriptTemplateView
构造器概要
构造器 构造器 说明 ScriptTemplateConfigurer()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 Charset
getCharset()
Return the charset used to read script and template files.String
getContentType()
Return the content type to use for the response.ScriptEngine
getEngine()
Return theScriptEngine
to use by the views.String
getEngineName()
Return the engine name that will be used to instantiate theScriptEngine
.String
getRenderFunction()
Return the render function name (mandatory).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 filesvoid
setContentType(String contentType)
Set the content type to use for the responsevoid
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
setRenderFunction(String renderFunction)
Set the render function name (mandatory).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.
构造器详细资料
ScriptTemplateConfigurer
public ScriptTemplateConfigurer()
方法详细资料
setEngine
public void setEngine(ScriptEngine engine)
Set theScriptEngine
to use by the view. 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 with thesetEngineName(String)
one (since it implies multiple lazy instantiations of the script engine).
getEngine
public ScriptEngine getEngine()
从接口复制的说明:ScriptTemplateConfig
Return theScriptEngine
to use by the views.- 指定者:
getEngine
在接口中ScriptTemplateConfig
setEngineName
public void setEngineName(String engineName)
Set the engine name that will be used to instantiate theScriptEngine
. The script engine must implementInvocable
. You must defineengine
orengineName
, not both.
getEngineName
public String getEngineName()
从接口复制的说明:ScriptTemplateConfig
Return the engine name that will be used to instantiate theScriptEngine
.- 指定者:
getEngineName
在接口中ScriptTemplateConfig
setSharedEngine
public void setSharedEngine(Boolean sharedEngine)
When set tofalse
, use thread-localScriptEngine
instances instead of one single shared instance. This flag should be set tofalse
for 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).
isSharedEngine
public Boolean isSharedEngine()
从接口复制的说明:ScriptTemplateConfig
Return whether to use a shared engine for all threads or whether to create thread-local engine instances for each thread.- 指定者:
isSharedEngine
在接口中ScriptTemplateConfig
setScripts
public void setScripts(String... scriptNames)
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
public String[] getScripts()
从接口复制的说明:ScriptTemplateConfig
Return the scripts to be loaded by the script engine (library or user provided).- 指定者:
getScripts
在接口中ScriptTemplateConfig
setRenderObject
public void setRenderObject(String renderObject)
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
public String getRenderObject()
从接口复制的说明:ScriptTemplateConfig
Return the object where the render function belongs (optional).- 指定者:
getRenderObject
在接口中ScriptTemplateConfig
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()
从接口复制的说明:ScriptTemplateConfig
Return the render function name (mandatory).- 指定者:
getRenderFunction
在接口中ScriptTemplateConfig
setContentType
public void setContentType(String contentType)
Set the content type to use for the response. (text/html
by default).- 从以下版本开始:
- 4.2.1
getContentType
public String getContentType()
Return the content type to use for the response.- 指定者:
getContentType
在接口中ScriptTemplateConfig
- 从以下版本开始:
- 4.2.1
setCharset
public void setCharset(Charset charset)
Set the charset used to read script and template files. (UTF-8
by default).
getCharset
public Charset getCharset()
从接口复制的说明:ScriptTemplateConfig
Return the charset used to read script and template files.- 指定者:
getCharset
在接口中ScriptTemplateConfig
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()
从接口复制的说明:ScriptTemplateConfig
Return the resource loader path(s) via a Spring resource location.