org.springframework.web.servlet.view.script

Class ScriptTemplateConfigurer

  • java.lang.Object
    • org.springframework.web.servlet.view.script.ScriptTemplateConfigurer
  • All Implemented Interfaces:
    ScriptTemplateConfig


    public class ScriptTemplateConfigurer
    extends java.lang.Object
    implements ScriptTemplateConfig
    An implementation of Spring MVC'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.

    Since:
    4.2
    Author:
    Sebastien Deleuze
    See Also:
    ScriptTemplateView
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      java.nio.charset.Charset getCharset()
      Return the charset used to read script and template files.
      java.lang.String getContentType()
      Return the content type to use for the response.
      javax.script.ScriptEngine getEngine()
      Return the ScriptEngine to use by the views.
      java.lang.String getEngineName()
      Return the engine name that will be used to instantiate the ScriptEngine.
      java.lang.String getRenderFunction()
      Return the render function name (optional).
      java.lang.String getRenderObject()
      Return the object where the render function belongs (optional).
      java.lang.String getResourceLoaderPath()
      Return the resource loader path(s) via a Spring resource location.
      java.lang.String[] getScripts()
      Return the scripts to be loaded by the script engine (library or user provided).
      java.lang.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(java.nio.charset.Charset charset)
      Set the charset used to read script and template files.
      void setContentType(java.lang.String contentType)
      Set the content type to use for the response.
      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 (optional).
      void setRenderObject(java.lang.String renderObject)
      Set the object where the render function belongs (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).
      void setSharedEngine(java.lang.Boolean sharedEngine)
      When set to false, use thread-local ScriptEngine instances instead of one single shared instance.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ScriptTemplateConfigurer

        public ScriptTemplateConfigurer()
        Default constructor.
      • ScriptTemplateConfigurer

        public ScriptTemplateConfigurer(java.lang.String engineName)
        Create a new ScriptTemplateConfigurer using the given engine name.
    • Method Detail

      • setEngine

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

        See Also:
        setEngineName(String)
      • setEngineName

        public void setEngineName(@Nullable
                                  java.lang.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.
        See Also:
        setEngine(ScriptEngine)
      • setSharedEngine

        public void setSharedEngine(@Nullable
                                    java.lang.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. 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 using setEngineName(String). Using setEngine(ScriptEngine) is not possible because multiple instances of the script engine need to be created lazily (one per thread).

        See Also:
        THREADING ScriptEngine parameter
      • setScripts

        public void setScripts(@Nullable
                               java.lang.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");.

        See Also:
        setResourceLoaderPath(java.lang.String), WebJars
      • setRenderObject

        public void setRenderObject(@Nullable
                                    java.lang.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".
      • setRenderFunction

        public void setRenderFunction(@Nullable
                                      java.lang.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)
        See Also:
        RenderingContext
      • getRenderFunction

        @Nullable
        public java.lang.String getRenderFunction()
        Description copied from interface: ScriptTemplateConfig
        Return the render function name (optional). If not specified, the script templates will be evaluated with ScriptEngine.eval(String, Bindings).
        Specified by:
        getRenderFunction in interface ScriptTemplateConfig
      • setContentType

        public void setContentType(@Nullable
                                   java.lang.String contentType)
        Set the content type to use for the response. (text/html by default).
        Since:
        4.2.1
      • setCharset

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

        public void setResourceLoaderPath(@Nullable
                                          java.lang.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's ResourceLoader. Relative paths are allowed when running in an ApplicationContext.

        Default is "classpath:".