org.springframework.web.servlet.view.script

## Class ScriptTemplateView

- java.lang.Object
- - org.springframework.context.support.ApplicationObjectSupport
  - - org.springframework.web.context.support.WebApplicationObjectSupport
    - - org.springframework.web.servlet.view.AbstractView
      - - org.springframework.web.servlet.view.AbstractUrlBasedView
        - - org.springframework.web.servlet.view.script.ScriptTemplateView

- All Implemented Interfaces:
    Aware, BeanNameAware, InitializingBean, ApplicationContextAware, ServletContextAware, View

      public class ScriptTemplateView
      extends AbstractUrlBasedView

  An `AbstractUrlBasedView` subclass designed to run any template library based on a JSR-223 script engine.

  If not set, each property is auto-detected by looking up a single `ScriptTemplateConfig` bean in the web application context and using it to obtain the configured properties.

  The Nashorn JavaScript engine requires Java 8+ and may require setting the `sharedEngine` property to `false` in order to run properly. See `ScriptTemplateConfigurer.setSharedEngine(Boolean)` for more details.

  Since:
    4.2
  Author:
    Sebastien Deleuze, Juergen Hoeller
  See Also:
    `ScriptTemplateConfigurer`, `ScriptTemplateViewResolver`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `DEFAULT_CONTENT_TYPE` The default content type for the view.

    - ### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

      `logger`

    - ### Fields inherited from interface org.springframework.web.servlet.View

      `PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ScriptTemplateView()` Constructor for use as a bean.
    `ScriptTemplateView(String url)` Create a new ScriptTemplateView with the given URL.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected ScriptTemplateConfig` | `autodetectViewConfig()`
    `boolean` | `checkResource(Locale locale)` Check whether the underlying resource that the configured URL points to actually exists.
    `protected ScriptEngine` | `createEngineFromName(String engineName)`
    `protected ScriptEngine` | `getEngine()`
    `protected Resource` | `getResource(String location)`
    `protected String` | `getTemplate(String path)`
    `protected void` | `initApplicationContext(ApplicationContext context)` Calls `WebApplicationObjectSupport.initServletContext(javax.servlet.ServletContext)` if the given ApplicationContext is a `WebApplicationContext`.
    `protected void` | `loadScripts(ScriptEngine engine)`
    `protected void` | `prepareResponse(HttpServletRequest request, HttpServletResponse response)` Prepare the given response for rendering.
    `protected void` | `renderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)` Subclasses must implement this method to actually render the view.
    `void` | `setCharset(Charset charset)` See `ScriptTemplateConfigurer.setCharset(Charset)` documentation.
    `void` | `setEngine(ScriptEngine engine)` See `ScriptTemplateConfigurer.setEngine(ScriptEngine)` documentation.
    `void` | `setEngineName(String engineName)` See `ScriptTemplateConfigurer.setEngineName(String)` documentation.
    `void` | `setEngineSupplier(Supplier<ScriptEngine> engineSupplier)` See `ScriptTemplateConfigurer.setEngineSupplier(Supplier)` documentation.
    `void` | `setRenderFunction(String functionName)` See `ScriptTemplateConfigurer.setRenderFunction(String)` documentation.
    `void` | `setRenderObject(String renderObject)` See `ScriptTemplateConfigurer.setRenderObject(String)` documentation.
    `void` | `setResourceLoaderPath(String resourceLoaderPath)` See `ScriptTemplateConfigurer.setResourceLoaderPath(String)` documentation.
    `void` | `setScripts(String... scripts)` See `ScriptTemplateConfigurer.setScripts(String...)` documentation.
    `void` | `setSharedEngine(Boolean sharedEngine)` See `ScriptTemplateConfigurer.setSharedEngine(Boolean)` documentation.

    - ### Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView

      `afterPropertiesSet, getUrl, isUrlRequired, setUrl, toString`

    - ### Methods inherited from class org.springframework.web.servlet.view.AbstractView

      `addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse`

    - ### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

      `getServletContext, getTempDir, getWebApplicationContext, initServletContext, isContextRequired, setServletContext`

    - ### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

      `getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_CONTENT_TYPE

          public static final String DEFAULT_CONTENT_TYPE

      The default content type for the view.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### ScriptTemplateView

          public ScriptTemplateView()

      Constructor for use as a bean.
      See Also:
        `AbstractUrlBasedView.setUrl(java.lang.String)`

    - #### ScriptTemplateView

          public ScriptTemplateView(String url)

      Create a new ScriptTemplateView with the given URL.
      Since:
        4.2.1

  - ### Method Detail

    - #### setEngine

          public void setEngine(ScriptEngine engine)

      See `ScriptTemplateConfigurer.setEngine(ScriptEngine)` documentation.

    - #### setEngineSupplier

          public void setEngineSupplier(Supplier<ScriptEngine> engineSupplier)

      See `ScriptTemplateConfigurer.setEngineSupplier(Supplier)` documentation.
      Since:
        5.2

    - #### setEngineName

          public void setEngineName(String engineName)

      See `ScriptTemplateConfigurer.setEngineName(String)` documentation.

    - #### setSharedEngine

          public void setSharedEngine(Boolean sharedEngine)

      See `ScriptTemplateConfigurer.setSharedEngine(Boolean)` documentation.

    - #### setScripts

          public void setScripts(String... scripts)

      See `ScriptTemplateConfigurer.setScripts(String...)` documentation.

    - #### setRenderObject

          public void setRenderObject(String renderObject)

      See `ScriptTemplateConfigurer.setRenderObject(String)` documentation.

    - #### setRenderFunction

          public void setRenderFunction(String functionName)

      See `ScriptTemplateConfigurer.setRenderFunction(String)` documentation.

    - #### setCharset

          public void setCharset(Charset charset)

      See `ScriptTemplateConfigurer.setCharset(Charset)` documentation.

    - #### setResourceLoaderPath

          public void setResourceLoaderPath(String resourceLoaderPath)

      See `ScriptTemplateConfigurer.setResourceLoaderPath(String)` documentation.

    - #### initApplicationContext

          protected void initApplicationContext(ApplicationContext context)

      Description copied from class: `WebApplicationObjectSupport`
      Calls `WebApplicationObjectSupport.initServletContext(javax.servlet.ServletContext)` if the given ApplicationContext is a `WebApplicationContext`.
      Overrides:
        `initApplicationContext` in class `WebApplicationObjectSupport`
      Parameters:
        `context` - the containing ApplicationContext
      See Also:
        `ApplicationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext)`

    - #### getEngine

          protected ScriptEngine getEngine()

    - #### createEngineFromName

          protected ScriptEngine createEngineFromName(String engineName)

    - #### loadScripts

          protected void loadScripts(ScriptEngine engine)

    - #### getResource

          @Nullable
          protected Resource getResource(String location)

    - #### autodetectViewConfig

          protected ScriptTemplateConfig autodetectViewConfig()
                                                       throws BeansException

      Throws:
        `BeansException`

    - #### checkResource

          public boolean checkResource(Locale locale)
                                throws Exception

      Description copied from class: `AbstractUrlBasedView`
      Check whether the underlying resource that the configured URL points to actually exists.
      Overrides:
        `checkResource` in class `AbstractUrlBasedView`
      Parameters:
        `locale` - the desired Locale that we're looking for
      Returns:
        `true` if the resource exists (or is assumed to exist); `false` if we know that it does not exist
      Throws:
        `Exception` - if the resource exists but is invalid (e.g. could not be parsed)

    - #### prepareResponse

          protected void prepareResponse(HttpServletRequest request,
                                         HttpServletResponse response)

      Description copied from class: `AbstractView`
      Prepare the given response for rendering.

      The default implementation applies a workaround for an IE bug when sending download content via HTTPS.

      Overrides:
        `prepareResponse` in class `AbstractView`
      Parameters:
        `request` - current HTTP request
        `response` - current HTTP response

    - #### renderMergedOutputModel

          protected void renderMergedOutputModel(Map<String,Object> model,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response)
                                          throws Exception

      Description copied from class: `AbstractView`
      Subclasses must implement this method to actually render the view.

      The first step will be preparing the request: In the JSP case, this would mean setting model objects as request attributes. The second step will be the actual rendering of the view, for example including the JSP via a RequestDispatcher.

      Specified by:
        `renderMergedOutputModel` in class `AbstractView`
      Parameters:
        `model` - combined output Map (never `null`), with dynamic values taking precedence over static attributes
        `request` - current HTTP request
        `response` - current HTTP response
      Throws:
        `Exception` - if rendering failed

    - #### getTemplate

          protected String getTemplate(String path)
                                throws IOException

      Throws:
        `IOException`