类 ScriptTemplateConfigurer

java.lang.Object
cn.taketoday.web.view.script.ScriptTemplateConfigurer
所有已实现的接口:
ScriptTemplateConfig

public class ScriptTemplateConfigurer extends Object implements ScriptTemplateConfig
An implementation of Framework Web's 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.

从以下版本开始:
4.0
作者:
Sebastien Deleuze
另请参阅:
  • 字段详细资料

    • engine

      @Nullable private ScriptEngine engine
    • engineSupplier

      @Nullable private Supplier<ScriptEngine> engineSupplier
    • engineName

      @Nullable private String engineName
    • sharedEngine

      @Nullable private Boolean sharedEngine
    • scripts

      @Nullable private String[] scripts
    • renderObject

      @Nullable private String renderObject
    • renderFunction

      @Nullable private String renderFunction
    • contentType

      @Nullable private String contentType
    • charset

      @Nullable private Charset charset
    • resourceLoaderPath

      @Nullable private String resourceLoaderPath
  • 构造器详细资料

    • ScriptTemplateConfigurer

      public ScriptTemplateConfigurer()
      Default constructor.
    • ScriptTemplateConfigurer

      public ScriptTemplateConfigurer(String engineName)
      Create a new ScriptTemplateConfigurer using the given engine name.
  • 方法详细资料

    • setEngine

      public void setEngine(@Nullable ScriptEngine engine)
      Set the 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 setEngineName(String) or setEngineSupplier(Supplier) since it implies multiple lazy instantiations of the script engine.

      另请参阅:
    • getEngine

      @Nullable public ScriptEngine getEngine()
      从接口复制的说明: ScriptTemplateConfig
      Return the ScriptEngine to use by the views.
      指定者:
      getEngine 在接口中 ScriptTemplateConfig
    • setEngineSupplier

      public void setEngineSupplier(@Nullable Supplier<ScriptEngine> engineSupplier)
      Set the 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.
      另请参阅:
    • getEngineSupplier

      @Nullable public Supplier<ScriptEngine> getEngineSupplier()
      从接口复制的说明: ScriptTemplateConfig
      Return the engine supplier that will be used to instantiate the ScriptEngine.
      指定者:
      getEngineSupplier 在接口中 ScriptTemplateConfig
    • setEngineName

      public void setEngineName(@Nullable String engineName)
      Set the engine name that will be used to instantiate the ScriptEngine. If renderFunction is specified, the script engine must implement Invocable. You must define engine or engineName, not both.
      另请参阅:
    • getEngineName

      @Nullable public String getEngineName()
      从接口复制的说明: ScriptTemplateConfig
      Return the engine name that will be used to instantiate the ScriptEngine.
      指定者:
      getEngineName 在接口中 ScriptTemplateConfig
    • setSharedEngine

      public void setSharedEngine(@Nullable Boolean sharedEngine)
      When set to false, use thread-local ScriptEngine instances instead of one single shared instance. 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) or setEngineSupplier(Supplier). Using setEngine(ScriptEngine) is not possible because multiple instances of the script engine need to be created lazily (one per thread).

      另请参阅:
    • isSharedEngine

      @Nullable 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(@Nullable String... scriptNames)
      Set the scripts to be loaded by the script engine (library or user provided). Since 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");.

      另请参阅:
    • getScripts

      @Nullable public String[] getScripts()
      从接口复制的说明: ScriptTemplateConfig
      Return the scripts to be loaded by the script engine (library or user provided).
      指定者:
      getScripts 在接口中 ScriptTemplateConfig
    • setRenderObject

      public void setRenderObject(@Nullable String renderObject)
      Set the object where the render function belongs (optional). For example, in order to call Mustache.render(), renderObject should be set to "Mustache" and renderFunction to "render".
    • getRenderObject

      @Nullable public String getRenderObject()
      从接口复制的说明: ScriptTemplateConfig
      Return the object where the render function belongs (optional).
      指定者:
      getRenderObject 在接口中 ScriptTemplateConfig
    • setRenderFunction

      public void setRenderFunction(@Nullable String renderFunction)
      Set the render function name (optional). If not specified, the script templates will be evaluated with ScriptEngine.eval(String, Bindings).

      This function will be called with the following parameters:

      1. String template: the template content
      2. Map model: the view model
      3. RenderingContext context: the rendering context (since 5.0)
      另请参阅:
    • getRenderFunction

      @Nullable public String getRenderFunction()
      从接口复制的说明: ScriptTemplateConfig
      Return the render function name (optional). If not specified, the script templates will be evaluated with ScriptEngine.eval(String, Bindings).
      指定者:
      getRenderFunction 在接口中 ScriptTemplateConfig
    • setContentType

      public void setContentType(@Nullable String contentType)
      Set the content type to use for the response. (text/html by default).
    • getContentType

      @Nullable public String getContentType()
      Return the content type to use for the response.
      指定者:
      getContentType 在接口中 ScriptTemplateConfig
    • setCharset

      public void setCharset(@Nullable Charset charset)
      Set the charset used to read script and template files. (UTF-8 by default).
    • getCharset

      @Nullable public Charset getCharset()
      从接口复制的说明: ScriptTemplateConfig
      Return the charset used to read script and template files.
      指定者:
      getCharset 在接口中 ScriptTemplateConfig
    • setResourceLoaderPath

      public void setResourceLoaderPath(@Nullable String resourceLoaderPath)
      Set the resource loader path(s) via a Framework 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 Framework's ResourceLoader. Relative paths are allowed when running in an ApplicationContext.

      Default is "classpath:".

    • getResourceLoaderPath

      @Nullable public String getResourceLoaderPath()
      从接口复制的说明: ScriptTemplateConfig
      Return the resource loader path(s) via a Framework resource location.
      指定者:
      getResourceLoaderPath 在接口中 ScriptTemplateConfig