public class ScriptTemplateView extends AbstractUrlBasedView
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.
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.
ScriptTemplateConfigurer
,
ScriptTemplateViewResolver
Modifier and Type | Class and Description |
---|---|
private static class |
ScriptTemplateView.EngineKey
Key class for the
enginesHolder ThreadLocal . |
Modifier and Type | Field and Description |
---|---|
private java.nio.charset.Charset |
charset |
private static java.nio.charset.Charset |
DEFAULT_CHARSET |
static java.lang.String |
DEFAULT_CONTENT_TYPE |
private static java.lang.String |
DEFAULT_RESOURCE_LOADER_PATH |
private javax.script.ScriptEngine |
engine |
private java.lang.String |
engineName |
private static java.lang.ThreadLocal<java.util.Map<java.lang.Object,javax.script.ScriptEngine>> |
enginesHolder |
private java.util.Locale |
locale |
private java.lang.String |
renderFunction |
private java.lang.String |
renderObject |
private ResourceLoader |
resourceLoader |
private java.lang.String[] |
resourceLoaderPaths |
private javax.script.ScriptEngineManager |
scriptEngineManager |
private java.lang.String[] |
scripts |
private java.lang.Boolean |
sharedEngine |
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
ScriptTemplateView()
Constructor for use as a bean.
|
ScriptTemplateView(java.lang.String url)
Create a new ScriptTemplateView with the given URL.
|
Modifier and Type | Method and Description |
---|---|
protected ScriptTemplateConfig |
autodetectViewConfig() |
boolean |
checkResource(java.util.Locale locale)
Check whether the underlying resource that the configured URL points to
actually exists.
|
protected javax.script.ScriptEngine |
createEngineFromName() |
protected javax.script.ScriptEngine |
getEngine() |
protected Resource |
getResource(java.lang.String location) |
protected java.lang.String |
getTemplate(java.lang.String path) |
protected void |
initApplicationContext(ApplicationContext context)
Calls
#initServletContext(javax.servlet.ServletContext) if the
given ApplicationContext is a WebApplicationContext . |
protected void |
loadScripts(javax.script.ScriptEngine engine) |
protected void |
prepareResponse(HttpServletRequest request,
HttpServletResponse response)
Prepare the given response for rendering.
|
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Subclasses must implement this method to actually render the view.
|
void |
setCharset(java.nio.charset.Charset charset)
See
ScriptTemplateConfigurer.setCharset(Charset) documentation. |
void |
setContentType(java.lang.String contentType)
See
ScriptTemplateConfigurer.setContentType(String) } documentation. |
void |
setEngine(javax.script.ScriptEngine engine)
See
ScriptTemplateConfigurer.setEngine(ScriptEngine) documentation. |
void |
setEngineName(java.lang.String engineName)
See
ScriptTemplateConfigurer.setEngineName(String) documentation. |
void |
setLocale(java.util.Locale locale)
Set the
Locale to pass to the render function. |
void |
setRenderFunction(java.lang.String functionName)
See
ScriptTemplateConfigurer.setRenderFunction(String) documentation. |
void |
setRenderObject(java.lang.String renderObject)
See
ScriptTemplateConfigurer.setRenderObject(String) documentation. |
void |
setResourceLoaderPath(java.lang.String resourceLoaderPath)
See
ScriptTemplateConfigurer.setResourceLoaderPath(String) documentation. |
void |
setScripts(java.lang.String... scripts)
See
ScriptTemplateConfigurer.setScripts(String...) documentation. |
void |
setSharedEngine(java.lang.Boolean sharedEngine)
See
ScriptTemplateConfigurer.setSharedEngine(Boolean) documentation. |
afterPropertiesSet, getUrl, isUrlRequired, setUrl, toString
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
public static final java.lang.String DEFAULT_CONTENT_TYPE
private static final java.nio.charset.Charset DEFAULT_CHARSET
private static final java.lang.String DEFAULT_RESOURCE_LOADER_PATH
private static final java.lang.ThreadLocal<java.util.Map<java.lang.Object,javax.script.ScriptEngine>> enginesHolder
private javax.script.ScriptEngine engine
private java.lang.String engineName
private java.util.Locale locale
private java.lang.Boolean sharedEngine
private java.lang.String[] scripts
private java.lang.String renderObject
private java.lang.String renderFunction
private java.nio.charset.Charset charset
private java.lang.String[] resourceLoaderPaths
private ResourceLoader resourceLoader
private volatile javax.script.ScriptEngineManager scriptEngineManager
public ScriptTemplateView()
public ScriptTemplateView(java.lang.String url)
public void setEngine(javax.script.ScriptEngine engine)
ScriptTemplateConfigurer.setEngine(ScriptEngine)
documentation.public void setLocale(java.util.Locale locale)
Locale
to pass to the render function.public void setEngineName(java.lang.String engineName)
ScriptTemplateConfigurer.setEngineName(String)
documentation.public void setSharedEngine(java.lang.Boolean sharedEngine)
ScriptTemplateConfigurer.setSharedEngine(Boolean)
documentation.public void setScripts(java.lang.String... scripts)
ScriptTemplateConfigurer.setScripts(String...)
documentation.public void setRenderObject(java.lang.String renderObject)
ScriptTemplateConfigurer.setRenderObject(String)
documentation.public void setRenderFunction(java.lang.String functionName)
ScriptTemplateConfigurer.setRenderFunction(String)
documentation.public void setContentType(java.lang.String contentType)
ScriptTemplateConfigurer.setContentType(String)
} documentation.setContentType
in class AbstractView
public void setCharset(java.nio.charset.Charset charset)
ScriptTemplateConfigurer.setCharset(Charset)
documentation.public void setResourceLoaderPath(java.lang.String resourceLoaderPath)
ScriptTemplateConfigurer.setResourceLoaderPath(String)
documentation.protected void initApplicationContext(ApplicationContext context)
WebApplicationObjectSupport
#initServletContext(javax.servlet.ServletContext)
if the
given ApplicationContext is a WebApplicationContext
.initApplicationContext
in class WebApplicationObjectSupport
context
- the containing ApplicationContextApplicationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext)
protected javax.script.ScriptEngine getEngine()
protected javax.script.ScriptEngine createEngineFromName()
protected void loadScripts(javax.script.ScriptEngine engine)
protected Resource getResource(java.lang.String location)
protected ScriptTemplateConfig autodetectViewConfig() throws BeansException
BeansException
public boolean checkResource(java.util.Locale locale) throws java.lang.Exception
AbstractUrlBasedView
checkResource
in class AbstractUrlBasedView
locale
- the desired Locale that we're looking fortrue
if the resource exists (or is assumed to exist);
false
if we know that it does not existjava.lang.Exception
- if the resource exists but is invalid (e.g. could not be parsed)protected void prepareResponse(HttpServletRequest request, HttpServletResponse response)
AbstractView
The default implementation applies a workaround for an IE bug when sending download content via HTTPS.
prepareResponse
in class AbstractView
request
- current HTTP requestresponse
- current HTTP responseprotected void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
AbstractView
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.
renderMergedOutputModel
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributesrequest
- current HTTP requestresponse
- current HTTP responsejava.lang.Exception
- if rendering failedprotected java.lang.String getTemplate(java.lang.String path) throws java.io.IOException
java.io.IOException