org.springframework.web.reactive.result.view.script

## Class ScriptTemplateConfigurer

- java.lang.Object
- - org.springframework.web.reactive.result.view.script.ScriptTemplateConfigurer

- All Implemented Interfaces:
    ScriptTemplateConfig

      public class ScriptTemplateConfigurer
      extends Object
      implements ScriptTemplateConfig

  An implementation of the Spring WebFlux `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:
    5.0
  Author:
    Sebastien Deleuze
  See Also:
    `ScriptTemplateView`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ScriptTemplateConfigurer()` Default constructor.
    `ScriptTemplateConfigurer(String engineName)` Create a new ScriptTemplateConfigurer using the given engine name.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Charset` | `getCharset()` Return the charset used to read script and template files.
    `ScriptEngine` | `getEngine()` Return the `ScriptEngine` to use by the views.
    `String` | `getEngineName()` Return the engine name that will be used to instantiate the `ScriptEngine`.
    `Supplier<ScriptEngine>` | `getEngineSupplier()` Return the engine supplier that will be used to instantiate the `ScriptEngine`.
    `String` | `getRenderFunction()` Return the render function name (optional).
    `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 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` | `setEngineSupplier(Supplier<ScriptEngine> engineSupplier)` Set the `ScriptEngine` supplier to use by the view, usually used with `setSharedEngine(Boolean)` set to `false`.
    `void` | `setRenderFunction(String renderFunction)` Set the render function name (optional).
    `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 to `false`, a new `ScriptEngine` instance will be created for each request, else the same instance will be reused.

    - ### 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(String engineName)

      Create a new ScriptTemplateConfigurer using the given engine name.

  - ### Method Detail

    - #### 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 the `setEngineName(String)` or `setEngineSupplier(Supplier)` (since it implies multiple lazy instantiations of the script engine).

      See Also:
        `setEngineName(String)`, `setEngineSupplier(Supplier)`

    - #### getEngine

          @Nullable
          public ScriptEngine getEngine()

      Description copied from interface: `ScriptTemplateConfig`
      Return the `ScriptEngine` to use by the views.
      Specified by:
        `getEngine` in interface `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`.
      Since:
        5.2
      See Also:
        `setEngine(ScriptEngine)`, `setEngineName(String)`

    - #### getEngineSupplier

          @Nullable
          public Supplier<ScriptEngine> getEngineSupplier()

      Description copied from interface: `ScriptTemplateConfig`
      Return the engine supplier that will be used to instantiate the `ScriptEngine`.
      Specified by:
        `getEngineSupplier` in interface `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.
      See Also:
        `setEngine(ScriptEngine)`, `setEngineSupplier(Supplier)`

    - #### getEngineName

          @Nullable
          public String getEngineName()

      Description copied from interface: `ScriptTemplateConfig`
      Return the engine name that will be used to instantiate the `ScriptEngine`.
      Specified by:
        `getEngineName` in interface `ScriptTemplateConfig`

    - #### setSharedEngine

          public void setSharedEngine(@Nullable
                                      Boolean sharedEngine)

      When set to `false`, a new `ScriptEngine` instance will be created for each request, else the same instance will be reused. 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)`. Using `setEngine(ScriptEngine)` is not possible because multiple instances of the script engine need to be created for each request.

      See Also:
        THREADING ScriptEngine parameter

    - #### isSharedEngine

          @Nullable
          public Boolean isSharedEngine()

      Description copied from interface: `ScriptTemplateConfig`
      Return whether to use a shared engine for all threads or whether to create thread-local engine instances for each thread.
      Specified by:
        `isSharedEngine` in interface `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");`.

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

    - #### getScripts

          @Nullable
          public String[] getScripts()

      Description copied from interface: `ScriptTemplateConfig`
      Return the scripts to be loaded by the script engine (library or user provided).
      Specified by:
        `getScripts` in interface `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()

      Description copied from interface: `ScriptTemplateConfig`
      Return the object where the render function belongs (optional).
      Specified by:
        `getRenderObject` in interface `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
      See Also:
        `RenderingContext`

    - #### getRenderFunction

          @Nullable
          public 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`

    - #### 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()

      Description copied from interface: `ScriptTemplateConfig`
      Return the charset used to read script and template files.
      Specified by:
        `getCharset` in interface `ScriptTemplateConfig`

    - #### setResourceLoaderPath

          public void setResourceLoaderPath(@Nullable
                                            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:".

    - #### getResourceLoaderPath

          @Nullable
          public String getResourceLoaderPath()

      Description copied from interface: `ScriptTemplateConfig`
      Return the resource loader path(s) via a Spring resource location.
      Specified by:
        `getResourceLoaderPath` in interface `ScriptTemplateConfig`