Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.view.script

public Class ScriptTemplateView

extends AbstractUrlBasedView
Class Inheritance
Imports
java.io.IOException, .InputStreamReader, java.nio.charset.Charset, .StandardCharsets, java.util.Arrays, .HashMap, .Locale, .Map, java.util.function.Function, .Supplier, javax.script.Invocable, .ScriptEngine, .ScriptEngineManager, .ScriptException, .SimpleBindings, jakarta.servlet.ServletException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanFactoryUtils, .NoSuchBeanDefinitionException, org.springframework.context.ApplicationContext, .ApplicationContextException, org.springframework.core.NamedThreadLocal, org.springframework.core.io.Resource, org.springframework.lang.Nullable, org.springframework.scripting.support.StandardScriptEvalException, .StandardScriptUtils, org.springframework.util.Assert, .FileCopyUtils, .ObjectUtils, .StringUtils, org.springframework.web.servlet.support.RequestContextUtils, org.springframework.web.servlet.view.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.

Authors
Sebastien Deleuze, Juergen Hoeller
Since
4.2
See Also
ScriptTemplateConfigurer, ScriptTemplateViewResolver

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ScriptTemplateView.EngineKey

Key class for the enginesHolder ThreadLocal.

Field Summary

Modifier and TypeField and Description
private Charset
private static final Charset
public static final String
private static final String
private ScriptEngine
private String
private static final ThreadLocal<Map<Object, ScriptEngine>>
private Supplier<ScriptEngine>
private String
private String
private String[]
private volatile ScriptEngineManager
private String[]
private Boolean

Constructor Summary

AccessConstructor and Description
public
ScriptTemplateView()

Constructor for use as a bean.

public
ScriptTemplateView(String url)

Create a new ScriptTemplateView with the given URL.

Method Summary

Modifier and TypeMethod and Description
protected ScriptTemplateConfig
public boolean
checkResource(Locale
the desired Locale that we're looking for
locale
)

Overrides org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

Check whether the underlying resource that the configured URL points to actually exists.

protected ScriptEngine
private ScriptEngine
protected ScriptEngine
protected Resource
getResource(String location)

protected String
protected void
protected void
protected void
prepareResponse(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Overrides org.springframework.web.servlet.view.AbstractView.prepareResponse.

Prepare the given response for rendering.

protected void
renderMergedOutputModel(Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Subclasses must implement this method to actually render the view.

public void
public void
public void
public void
public void
public void
public void
public void
public void
Inherited from org.springframework.web.servlet.view.AbstractUrlBasedView:
afterPropertiesSetgetUrlisUrlRequiredsetUrltoString

Field Detail

charsetback to summary
private Charset charset
Annotations
@Nullable
DEFAULT_CHARSETback to summary
private static final Charset DEFAULT_CHARSET
DEFAULT_CONTENT_TYPEback to summary
public static final String DEFAULT_CONTENT_TYPE

Hides org.springframework.web.servlet.view.AbstractView.DEFAULT_CONTENT_TYPE.

The default content type for the view.

DEFAULT_RESOURCE_LOADER_PATHback to summary
private static final String DEFAULT_RESOURCE_LOADER_PATH
engineback to summary
private ScriptEngine engine
Annotations
@Nullable
engineNameback to summary
private String engineName
Annotations
@Nullable
enginesHolderback to summary
private static final ThreadLocal<Map<Object, ScriptEngine>> enginesHolder
engineSupplierback to summary
private Supplier<ScriptEngine> engineSupplier
Annotations
@Nullable
renderFunctionback to summary
private String renderFunction
Annotations
@Nullable
renderObjectback to summary
private String renderObject
Annotations
@Nullable
resourceLoaderPathsback to summary
private String[] resourceLoaderPaths
Annotations
@Nullable
scriptEngineManagerback to summary
private volatile ScriptEngineManager scriptEngineManager
Annotations
@Nullable
scriptsback to summary
private String[] scripts
Annotations
@Nullable
sharedEngineback to summary
private Boolean sharedEngine
Annotations
@Nullable

Constructor Detail

ScriptTemplateViewback to summary
public ScriptTemplateView()

Constructor for use as a bean.

See Also
setUrl
ScriptTemplateViewback to summary
public ScriptTemplateView(String url)

Create a new ScriptTemplateView with the given URL.

Since
4.2.1

Method Detail

autodetectViewConfigback to summary
protected ScriptTemplateConfig autodetectViewConfig() throws BeansException
checkResourceback to summary
public boolean checkResource(Locale locale) throws Exception

Overrides org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

Doc from org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

Check whether the underlying resource that the configured URL points to actually exists.

Parameters
locale:Locale

the desired Locale that we're looking for

Returns:boolean

true if the resource exists (or is assumed to exist); false if we know that it does not exist

Annotations
@Override
Exceptions
Exception:
if the resource exists but is invalid (e.g. could not be parsed)
createEngineFromNameback to summary
protected ScriptEngine createEngineFromName(String engineName)
createEngineFromSupplierback to summary
private ScriptEngine createEngineFromSupplier()
getEngineback to summary
protected ScriptEngine getEngine()
getResourceback to summary
protected Resource getResource(String location)
Annotations
@Nullable
getTemplateback to summary
protected String getTemplate(String path) throws IOException
initApplicationContextback to summary
protected void initApplicationContext(ApplicationContext context)

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext.

Doc from org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext.

Calls initServletContext(jakarta.servlet.ServletContext) if the given ApplicationContext is a WebApplicationContext.

Parameters
context:ApplicationContext

Doc from org.springframework.context.support.ApplicationObjectSupport.initApplicationContext.

the containing ApplicationContext

Annotations
@Override
loadScriptsback to summary
protected void loadScripts(ScriptEngine engine)
prepareResponseback to summary
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response)

Overrides org.springframework.web.servlet.view.AbstractView.prepareResponse.

Doc from org.springframework.web.servlet.view.AbstractView.prepareResponse.

Prepare the given response for rendering.

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

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

Annotations
@Override
renderMergedOutputModelback to summary
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

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.

Parameters
model:Map<String, Object>

combined output Map (never null), with dynamic values taking precedence over static attributes

request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

Annotations
@Override
Exceptions
Exception:
if rendering failed
setCharsetback to summary
public void setCharset(Charset charset)

See ScriptTemplateConfigurer#setCharset(Charset) documentation.

setEngineback to summary
public void setEngine(ScriptEngine engine)

See ScriptTemplateConfigurer#setEngine(ScriptEngine) documentation.

setEngineNameback to summary
public void setEngineName(String engineName)

See ScriptTemplateConfigurer#setEngineName(String) documentation.

setEngineSupplierback to summary
public void setEngineSupplier(Supplier<ScriptEngine> engineSupplier)

See ScriptTemplateConfigurer#setEngineSupplier(Supplier) documentation.

Since
5.2
setRenderFunctionback to summary
public void setRenderFunction(String functionName)

See ScriptTemplateConfigurer#setRenderFunction(String) documentation.

setRenderObjectback to summary
public void setRenderObject(String renderObject)

See ScriptTemplateConfigurer#setRenderObject(String) documentation.

setResourceLoaderPathback to summary
public void setResourceLoaderPath(String resourceLoaderPath)

See ScriptTemplateConfigurer#setResourceLoaderPath(String) documentation.

setScriptsback to summary
public void setScripts(String... scripts)

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

setSharedEngineback to summary
public void setSharedEngine(Boolean sharedEngine)

See ScriptTemplateConfigurer#setSharedEngine(Boolean) documentation.

org.springframework.web.servlet.view.script back to summary

private Class ScriptTemplateView.EngineKey

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.view.script.ScriptTemplateView.EngineKey

Key class for the enginesHolder ThreadLocal. Only used if scripts have been specified; otherwise, the engineName String will be used as cache key directly.

Field Summary

Modifier and TypeField and Description
private final String
private final String[]

Constructor Summary

AccessConstructor and Description
public
EngineKey(String engineName, String[] scripts)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

engineNameback to summary
private final String engineName
scriptsback to summary
private final String[] scripts

Constructor Detail

EngineKeyback to summary
public EngineKey(String engineName, String[] scripts)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override