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

public abstract Class AbstractView

extends WebApplicationObjectSupport
implements View, BeanNameAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.Aware, org.springframework.web.servlet.View
Known Direct Subclasses
org.springframework.web.servlet.view.document.AbstractPdfView, org.springframework.web.servlet.view.document.AbstractXlsView, org.springframework.web.servlet.view.feed.AbstractFeedView, org.springframework.web.servlet.view.json.AbstractJackson2View, org.springframework.web.servlet.view.xml.MarshallingView, org.springframework.web.servlet.view.AbstractUrlBasedView
Imports
java.io.ByteArrayOutputStream, .IOException, java.util.Collections, .LinkedHashMap, .Map, .Properties, .Set, .StringTokenizer, jakarta.servlet.ServletOutputStream, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.beans.factory.BeanNameAware, org.springframework.http.MediaType, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, org.springframework.web.context.WebApplicationContext, org.springframework.web.context.support.ContextExposingHttpServletRequest, .WebApplicationObjectSupport, org.springframework.web.servlet.View, org.springframework.web.servlet.support.RequestContext

Abstract base class for org.springframework.web.servlet.View implementations. Subclasses should be JavaBeans, to allow for convenient configuration as Spring-managed bean instances.

Provides support for static attributes, to be made available to the view, with a variety of ways to specify them. Static attributes will be merged with the given dynamic attributes (the model that the controller returned) for each render operation.

Extends WebApplicationObjectSupport, which will be helpful to some views. Subclasses just need to implement the actual rendering.

Authors
Rod Johnson, Juergen Hoeller
See Also
setAttributes, setAttributesMap, renderMergedOutputModel

Field Summary

Modifier and TypeField and Description
private String
private String
public static final String
DEFAULT_CONTENT_TYPE

Default content type.

private boolean
private Set<String>
private boolean
private static final int
OUTPUT_BYTE_ARRAY_INITIAL_SIZE

Initial size for the temporary output byte array (if any).

private String
private final Map<String, Object>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addStaticAttribute(String
the name of the attribute to expose
name
,
Object
the attribute value to expose
value
)

Add static data to this view, exposed in each view.

protected Map<String, Object>
createMergedOutputModel(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)

Creates a combined output Map (never null) that includes dynamic values and static attributes.

protected RequestContext

Returns:

the RequestContext instance
createRequestContext
(HttpServletRequest
current HTTP request
request
,
HttpServletResponse response, Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
model
)

Create a RequestContext to expose under the specified attribute name.

protected ByteArrayOutputStream
createTemporaryOutputStream()

Create a temporary OutputStream for this view.

protected void
exposeModelAsRequestAttributes(Map<String, Object>
a Map of model objects to expose
model
,
HttpServletRequest
current HTTP request
request
)

Expose the model objects in the given map as request attributes.

protected String
protected boolean
generatesDownloadContent()

Return whether this view generates download content (typically binary content like PDF or Excel files).

public Map<String, Object>
getAttributesMap()

Allow Map access to the static attributes of this view, with the option to add or override specific entries.

public String
getBeanName()

Return the view's name.

public String
getContentType()

Overrides default org.springframework.web.servlet.View.getContentType.

Return the content type for this view.

public String
getRequestContextAttribute()

Return the name of the RequestContext attribute, if any.

protected HttpServletRequest

Returns:

the wrapped request, or the original request if no wrapping is necessary
getRequestToExpose
(HttpServletRequest
the original servlet request as provided by the engine
originalRequest
)

Get the request handle to expose to renderMergedOutputModel, i.e. to the view.

public Map<String, Object>

Returns:

the static attributes in this view
getStaticAttributes
()

Return the static attributes for this view.

public boolean
isExposePathVariables()

Return whether to add path variables to the model or not.

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

Prepare the given response for rendering.

public void
render(Map<String, ?>
a Map with name Strings as keys and corresponding model objects as values (Map can also be null in case of empty model)
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
he HTTP response we are building
response
)

Implements org.springframework.web.servlet.View.render.

Prepares the view given the specified model, merging it with static attributes and a RequestContext attribute, if necessary.

protected abstract 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
)

Subclasses must implement this method to actually render the view.

public void
setAttributes(Properties attributes)

Set static attributes for this view from a java.util.Properties object.

public void
setAttributesCSV(String propString)

Set static attributes as a CSV string.

public void
setAttributesMap(Map<String, ?>
a Map with name Strings as keys and attribute objects as values
attributes
)

Set static attributes for this view from a Map.

public void
setBeanName(String
the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.
beanName
)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

Set the view's name.

public void
setContentType(String contentType)

Set the content type for this view.

public void
setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes)

Set whether to make all Spring beans in the application context accessible as request attributes, through lazy checking once an attribute gets accessed.

public void
setExposedContextBeanNames(String... exposedContextBeanNames)

Specify the names of beans in the context which are supposed to be exposed.

public void
setExposePathVariables(boolean
true to expose path variables, and false otherwise
exposePathVariables
)

Specify whether to add path variables to the model or not.

public void
setRequestContextAttribute(String requestContextAttribute)

Set the name of the RequestContext attribute for this view.

protected void
setResponseContentType(HttpServletRequest request, HttpServletResponse response)

Set the content type of the response to the configured content type unless the View#SELECTED_CONTENT_TYPE request attribute is present and set to a concrete media type.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

protected void
writeToResponse(HttpServletResponse
current HTTP response
response
,
ByteArrayOutputStream
the temporary OutputStream to write
baos
)

Write the given temporary OutputStream to the HTTP response.

Inherited from org.springframework.web.context.support.WebApplicationObjectSupport:
getServletContextgetTempDirgetWebApplicationContextinitApplicationContextinitServletContextisContextRequiredsetServletContext

Field Detail

beanNameback to summary
private String beanName
Annotations
@Nullable
contentTypeback to summary
private String contentType
Annotations
@Nullable
DEFAULT_CONTENT_TYPEback to summary
public static final String DEFAULT_CONTENT_TYPE

Default content type. Overridable as bean property.

exposeContextBeansAsAttributesback to summary
private boolean exposeContextBeansAsAttributes
exposedContextBeanNamesback to summary
private Set<String> exposedContextBeanNames
Annotations
@Nullable
exposePathVariablesback to summary
private boolean exposePathVariables
OUTPUT_BYTE_ARRAY_INITIAL_SIZEback to summary
private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE

Initial size for the temporary output byte array (if any).

requestContextAttributeback to summary
private String requestContextAttribute
Annotations
@Nullable
staticAttributesback to summary
private final Map<String, Object> staticAttributes

Constructor Detail

AbstractViewback to summary
public AbstractView()

Method Detail

addStaticAttributeback to summary
public void addStaticAttribute(String name, Object value)

Add static data to this view, exposed in each view.

"Static" attributes are fixed attributes that are specified in the View instance configuration. "Dynamic" attributes, on the other hand, are values passed in as part of the model.

Must be invoked before any calls to render.

Parameters
name:String

the name of the attribute to expose

value:Object

the attribute value to expose

See Also
render
createMergedOutputModelback to summary
protected Map<String, Object> createMergedOutputModel(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)

Creates a combined output Map (never null) that includes dynamic values and static attributes. Dynamic values take precedence over static attributes.

createRequestContextback to summary
protected RequestContext createRequestContext(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model)

Create a RequestContext to expose under the specified attribute name.

The default implementation creates a standard RequestContext instance for the given request and model. Can be overridden in subclasses for custom instances.

Parameters
request:HttpServletRequest

current HTTP request

model:Map<String, Object>

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

Returns:RequestContext

the RequestContext instance

See Also
setRequestContextAttribute, org.springframework.web.servlet.support.RequestContext
createTemporaryOutputStreamback to summary
protected ByteArrayOutputStream createTemporaryOutputStream()

Create a temporary OutputStream for this view.

This is typically used as IE workaround, for setting the content length header from the temporary stream before actually writing the content to the HTTP response.

exposeModelAsRequestAttributesback to summary
protected void exposeModelAsRequestAttributes(Map<String, Object> model, HttpServletRequest request) throws Exception

Expose the model objects in the given map as request attributes. Names will be taken from the model Map. This method is suitable for all resources reachable by jakarta.servlet.RequestDispatcher.

Parameters
model:Map<String, Object>

a Map of model objects to expose

request:HttpServletRequest

current HTTP request

formatViewNameback to summary
protected String formatViewName()
generatesDownloadContentback to summary
protected boolean generatesDownloadContent()

Return whether this view generates download content (typically binary content like PDF or Excel files).

The default implementation returns false. Subclasses are encouraged to return true here if they know that they are generating download content that requires temporary caching on the client side, typically via the response OutputStream.

See Also
prepareResponse, jakarta.servlet.http.HttpServletResponse#getOutputStream()
getAttributesMapback to summary
public Map<String, Object> getAttributesMap()

Allow Map access to the static attributes of this view, with the option to add or override specific entries.

Useful for specifying entries directly, for example via attributesMap[myKey]. This is particularly useful for adding or overriding entries in child view definitions.

getBeanNameback to summary
public String getBeanName()

Return the view's name. Should never be null, if the view was correctly configured.

Annotations
@Nullable
getContentTypeback to summary
public String getContentType()

Overrides default org.springframework.web.servlet.View.getContentType.

Return the content type for this view.

Returns:String

Doc from org.springframework.web.servlet.View.getContentType.

the content type String (optionally including a character set), or null if not predetermined

Annotations
@Override
@Nullable
getRequestContextAttributeback to summary
public String getRequestContextAttribute()

Return the name of the RequestContext attribute, if any.

Annotations
@Nullable
getRequestToExposeback to summary
protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest)

Get the request handle to expose to renderMergedOutputModel, i.e. to the view.

The default implementation wraps the original request for exposure of Spring beans as request attributes (if demanded).

Parameters
originalRequest:HttpServletRequest

the original servlet request as provided by the engine

Returns:HttpServletRequest

the wrapped request, or the original request if no wrapping is necessary

See Also
setExposeContextBeansAsAttributes, setExposedContextBeanNames, org.springframework.web.context.support.ContextExposingHttpServletRequest
getStaticAttributesback to summary
public Map<String, Object> getStaticAttributes()

Return the static attributes for this view. Handy for testing.

Returns an unmodifiable Map, as this is not intended for manipulating the Map but rather just for checking the contents.

Returns:Map<String, Object>

the static attributes in this view

isExposePathVariablesback to summary
public boolean isExposePathVariables()

Return whether to add path variables to the model or not.

prepareResponseback to summary
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response)

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

renderback to summary
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception

Implements org.springframework.web.servlet.View.render.

Prepares the view given the specified model, merging it with static attributes and a RequestContext attribute, if necessary. Delegates to renderMergedOutputModel for the actual rendering.

Parameters
model:Map<String, ?>

Doc from org.springframework.web.servlet.View.render.

a Map with name Strings as keys and corresponding model objects as values (Map can also be null in case of empty model)

request:HttpServletRequest

Doc from org.springframework.web.servlet.View.render.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.View.render.

he HTTP response we are building

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.servlet.View.render.

if rendering failed

See Also
renderMergedOutputModel
renderMergedOutputModelback to summary
protected abstract void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception

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

Exceptions
Exception:
if rendering failed
setAttributesback to summary
public void setAttributes(Properties attributes)

Set static attributes for this view from a java.util.Properties object.

"Static" attributes are fixed attributes that are specified in the View instance configuration. "Dynamic" attributes, on the other hand, are values passed in as part of the model.

This is the most convenient way to set static attributes. Note that static attributes can be overridden by dynamic attributes, if a value with the same name is included in the model.

Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.

See Also
org.springframework.beans.propertyeditors.PropertiesEditor
setAttributesCSVback to summary
public void setAttributesCSV(String propString) throws IllegalArgumentException

Set static attributes as a CSV string. Format is: attname0={value1},attname1={value1}

"Static" attributes are fixed attributes that are specified in the View instance configuration. "Dynamic" attributes, on the other hand, are values passed in as part of the model.

setAttributesMapback to summary
public void setAttributesMap(Map<String, ?> attributes)

Set static attributes for this view from a Map. This allows to set any kind of attribute values, for example bean references.

"Static" attributes are fixed attributes that are specified in the View instance configuration. "Dynamic" attributes, on the other hand, are values passed in as part of the model.

Can be populated with a "map" or "props" element in XML bean definitions.

Parameters
attributes:Map<String, ?>

a Map with name Strings as keys and attribute objects as values

setBeanNameback to summary
public void setBeanName(String beanName)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

Set the view's name. Helpful for traceability.

Framework code must call this when constructing views.

Parameters
beanName:String

Doc from org.springframework.beans.factory.BeanNameAware.setBeanName.

the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.

Annotations
@Override
setContentTypeback to summary
public void setContentType(String contentType)

Set the content type for this view. Default is "text/html;charset=ISO-8859-1".

May be ignored by subclasses if the view itself is assumed to set the content type, e.g. in case of JSPs.

setExposeContextBeansAsAttributesback to summary
public void setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes)

Set whether to make all Spring beans in the application context accessible as request attributes, through lazy checking once an attribute gets accessed.

This will make all such beans accessible in plain ${...} expressions in a JSP 2.0 page, as well as in JSTL's c:out value expressions.

Default is "false". Switch this flag on to transparently expose all Spring beans in the request attribute namespace.

Note

Context beans will override any custom request or session attributes of the same name that have been manually added. However, model attributes (as explicitly exposed to this view) of the same name will always override context beans.

See Also
getRequestToExpose
setExposedContextBeanNamesback to summary
public void setExposedContextBeanNames(String... exposedContextBeanNames)

Specify the names of beans in the context which are supposed to be exposed. If this is non-null, only the specified beans are eligible for exposure as attributes.

If you'd like to expose all Spring beans in the application context, switch the "exposeContextBeansAsAttributes" flag on but do not list specific bean names for this property.

setExposePathVariablesback to summary
public void setExposePathVariables(boolean exposePathVariables)

Specify whether to add path variables to the model or not.

Path variables are commonly bound to URI template variables through the @PathVariable annotation. They are effectively URI template variables with type conversion applied to them to derive typed Object values. Such values are frequently needed in views for constructing links to the same and other URLs.

Path variables added to the model override static attributes (see setAttributes(Properties)) but not attributes already present in the model.

By default this flag is set to true. Concrete view types can override this.

Parameters
exposePathVariables:boolean

true to expose path variables, and false otherwise

setRequestContextAttributeback to summary
public void setRequestContextAttribute(String requestContextAttribute)

Set the name of the RequestContext attribute for this view. Default is none.

setResponseContentTypeback to summary
protected void setResponseContentType(HttpServletRequest request, HttpServletResponse response)

Set the content type of the response to the configured content type unless the View#SELECTED_CONTENT_TYPE request attribute is present and set to a concrete media type.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
writeToResponseback to summary
protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException

Write the given temporary OutputStream to the HTTP response.

Parameters
response:HttpServletResponse

current HTTP response

baos:ByteArrayOutputStream

the temporary OutputStream to write

Exceptions
IOException:
if writing/flushing failed