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.
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.
ScriptTemplateConfigurer
,
ScriptTemplateViewResolver
logger, REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
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 |
checkResourceExists(java.util.Locale locale)
Check whether the resource for the configured URL actually exists.
|
protected javax.script.ScriptEngine |
createEngineFromName(java.lang.String engineName) |
protected javax.script.ScriptEngine |
getEngine() |
protected Resource |
getResource(java.lang.String location) |
protected java.lang.String |
getTemplate(java.lang.String path) |
protected void |
loadScripts(javax.script.ScriptEngine engine) |
protected reactor.core.publisher.Mono<java.lang.Void> |
renderInternal(java.util.Map<java.lang.String,java.lang.Object> model,
MediaType contentType,
ServerWebExchange exchange)
Subclasses must implement this method to actually render the view.
|
void |
setApplicationContext(ApplicationContext context)
Set the ApplicationContext that this object runs in.
|
void |
setEngine(javax.script.ScriptEngine engine)
See
ScriptTemplateConfigurer.setEngine(ScriptEngine) documentation. |
void |
setEngineName(java.lang.String engineName)
See
ScriptTemplateConfigurer.setEngineName(String) documentation. |
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, setUrl, toString
createRequestContext, formatViewName, getApplicationContext, getBeanName, getDefaultCharset, getModelAttributes, getRequestContextAttribute, getRequestDataValueProcessor, getSupportedMediaTypes, obtainApplicationContext, render, resolveAsyncAttributes, setBeanName, setDefaultCharset, setRequestContextAttribute, setSupportedMediaTypes
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
isRedirectView
public ScriptTemplateView()
public ScriptTemplateView(java.lang.String url)
public void setEngine(javax.script.ScriptEngine engine)
ScriptTemplateConfigurer.setEngine(ScriptEngine)
documentation.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 setResourceLoaderPath(java.lang.String resourceLoaderPath)
ScriptTemplateConfigurer.setResourceLoaderPath(String)
documentation.public void setApplicationContext(@Nullable ApplicationContext context)
ApplicationContextAware
Invoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and
MessageSourceAware
, if applicable.
setApplicationContext
in interface ApplicationContextAware
setApplicationContext
in class AbstractView
context
- the ApplicationContext object to be used by this objectBeanInitializationException
protected javax.script.ScriptEngine getEngine()
protected javax.script.ScriptEngine createEngineFromName(java.lang.String engineName)
protected void loadScripts(javax.script.ScriptEngine engine)
protected ScriptTemplateConfig autodetectViewConfig() throws BeansException
BeansException
public boolean checkResourceExists(java.util.Locale locale) throws java.lang.Exception
AbstractUrlBasedView
checkResourceExists
in class AbstractUrlBasedView
locale
- the desired Locale that we're looking forfalse
if the resource exists
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 reactor.core.publisher.Mono<java.lang.Void> renderInternal(java.util.Map<java.lang.String,java.lang.Object> model, @Nullable MediaType contentType, ServerWebExchange exchange)
AbstractView
renderInternal
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributescontentType
- the content type selected to render with which should
match one of the supported media types
.exchange
- current exchange @return Mono
to represent when
and if rendering succeedsprotected java.lang.String getTemplate(java.lang.String path) throws java.io.IOException
java.io.IOException