public abstract class AbstractView extends WebApplicationObjectSupport implements View, BeanNameAware
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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_CONTENT_TYPE
Default content type.
|
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
AbstractView() |
Modifier and Type | Method and Description |
---|---|
void |
addStaticAttribute(java.lang.String name,
java.lang.Object value)
Add static data to this view, exposed in each view.
|
protected java.util.Map<java.lang.String,java.lang.Object> |
createMergedOutputModel(java.util.Map<java.lang.String,?> model,
HttpServletRequest request,
HttpServletResponse response)
Creates a combined output Map (never
null ) that includes dynamic values and static attributes. |
protected RequestContext |
createRequestContext(HttpServletRequest request,
HttpServletResponse response,
java.util.Map<java.lang.String,java.lang.Object> model)
Create a RequestContext to expose under the specified attribute name.
|
protected java.io.ByteArrayOutputStream |
createTemporaryOutputStream()
Create a temporary OutputStream for this view.
|
protected void |
exposeModelAsRequestAttributes(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request)
Expose the model objects in the given map as request attributes.
|
protected boolean |
generatesDownloadContent()
Return whether this view generates download content
(typically binary content like PDF or Excel files).
|
java.util.Map<java.lang.String,java.lang.Object> |
getAttributesMap()
Allow Map access to the static attributes of this view,
with the option to add or override specific entries.
|
java.lang.String |
getBeanName()
Return the view's name.
|
java.lang.String |
getContentType()
Return the content type for this view.
|
java.lang.String |
getRequestContextAttribute()
Return the name of the RequestContext attribute, if any.
|
protected HttpServletRequest |
getRequestToExpose(HttpServletRequest originalRequest)
Get the request handle to expose to
renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) , i.e. |
java.util.Map<java.lang.String,java.lang.Object> |
getStaticAttributes()
Return the static attributes for this view.
|
boolean |
isExposePathVariables()
Return whether to add path variables to the model or not.
|
protected void |
prepareResponse(HttpServletRequest request,
HttpServletResponse response)
Prepare the given response for rendering.
|
void |
render(java.util.Map<java.lang.String,?> model,
HttpServletRequest request,
HttpServletResponse response)
Prepares the view given the specified model, merging it with static
attributes and a RequestContext attribute, if necessary.
|
protected abstract 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 |
setAttributes(java.util.Properties attributes)
Set static attributes for this view from a
java.util.Properties object. |
void |
setAttributesCSV(java.lang.String propString)
Set static attributes as a CSV string.
|
void |
setAttributesMap(java.util.Map<java.lang.String,?> attributes)
Set static attributes for this view from a Map.
|
void |
setBeanName(java.lang.String beanName)
Set the view's name.
|
void |
setContentType(java.lang.String contentType)
Set the content type for this view.
|
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.
|
void |
setExposedContextBeanNames(java.lang.String... exposedContextBeanNames)
Specify the names of beans in the context which are supposed to be exposed.
|
void |
setExposePathVariables(boolean exposePathVariables)
Specify whether to add path variables to the model or not.
|
void |
setRequestContextAttribute(java.lang.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. |
java.lang.String |
toString() |
protected void |
writeToResponse(HttpServletResponse response,
java.io.ByteArrayOutputStream baos)
Write the given temporary OutputStream to the HTTP response.
|
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
public static final java.lang.String DEFAULT_CONTENT_TYPE
public void setContentType(@Nullable java.lang.String contentType)
May be ignored by subclasses if the view itself is assumed to set the content type, e.g. in case of JSPs.
@Nullable public java.lang.String getContentType()
getContentType
in interface View
null
if not predeterminedpublic void setRequestContextAttribute(@Nullable java.lang.String requestContextAttribute)
@Nullable public java.lang.String getRequestContextAttribute()
public void setAttributesCSV(@Nullable java.lang.String propString) throws java.lang.IllegalArgumentException
"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.
java.lang.IllegalArgumentException
public void setAttributes(java.util.Properties attributes)
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.
PropertiesEditor
public void setAttributesMap(@Nullable java.util.Map<java.lang.String,?> attributes)
"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.
attributes
- a Map with name Strings as keys and attribute objects as valuespublic java.util.Map<java.lang.String,java.lang.Object> getAttributesMap()
Useful for specifying entries directly, for example via "attributesMap[myKey]". This is particularly useful for adding or overriding entries in child view definitions.
public void addStaticAttribute(java.lang.String name, java.lang.Object value)
"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
.
name
- the name of the attribute to exposevalue
- the attribute value to exposerender(java.util.Map<java.lang.String, ?>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
public java.util.Map<java.lang.String,java.lang.Object> getStaticAttributes()
Returns an unmodifiable Map, as this is not intended for manipulating the Map but rather just for checking the contents.
public void setExposePathVariables(boolean exposePathVariables)
Path variables are commonly bound to URI template variables through the @PathVariable
annotation. They're 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.
exposePathVariables
- true
to expose path variables, and false
otherwisepublic boolean isExposePathVariables()
public void setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes)
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.
public void setExposedContextBeanNames(java.lang.String... exposedContextBeanNames)
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.
public void setBeanName(@Nullable java.lang.String beanName)
Framework code must call this when constructing views.
setBeanName
in interface BeanNameAware
beanName
- 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.@Nullable public java.lang.String getBeanName()
null
,
if the view was correctly configured.public void render(@Nullable java.util.Map<java.lang.String,?> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
render
in interface View
model
- Map with name Strings as keys and corresponding model
objects as values (Map can also be null
in case of empty model)request
- current HTTP requestresponse
- HTTP response we are buildingjava.lang.Exception
- if rendering failedrenderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected java.util.Map<java.lang.String,java.lang.Object> createMergedOutputModel(@Nullable java.util.Map<java.lang.String,?> model, HttpServletRequest request, HttpServletResponse response)
null
) that includes dynamic values and static attributes.
Dynamic values take precedence over static attributes.protected RequestContext createRequestContext(HttpServletRequest request, HttpServletResponse response, java.util.Map<java.lang.String,java.lang.Object> model)
The default implementation creates a standard RequestContext instance for the given request and model. Can be overridden in subclasses for custom instances.
request
- current HTTP requestmodel
- combined output Map (never null
),
with dynamic values taking precedence over static attributessetRequestContextAttribute(java.lang.String)
,
RequestContext
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response)
The default implementation applies a workaround for an IE bug when sending download content via HTTPS.
request
- current HTTP requestresponse
- current HTTP responseprotected boolean generatesDownloadContent()
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.
protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest)
renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
, i.e. to the view.
The default implementation wraps the original request for exposure of Spring beans as request attributes (if demanded).
originalRequest
- the original servlet request as provided by the enginesetExposeContextBeansAsAttributes(boolean)
,
setExposedContextBeanNames(java.lang.String...)
,
ContextExposingHttpServletRequest
protected abstract void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
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.
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 void exposeModelAsRequestAttributes(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request) throws java.lang.Exception
RequestDispatcher
.model
- a Map of model objects to exposerequest
- current HTTP requestjava.lang.Exception
protected java.io.ByteArrayOutputStream createTemporaryOutputStream()
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.
protected void writeToResponse(HttpServletResponse response, java.io.ByteArrayOutputStream baos) throws java.io.IOException
response
- current HTTP responsebaos
- the temporary OutputStream to writejava.io.IOException
- if writing/flushing failedprotected void setResponseContentType(HttpServletRequest request, HttpServletResponse response)
content type
unless the
View.SELECTED_CONTENT_TYPE
request attribute is present and set
to a concrete media type.public java.lang.String toString()
toString
in class java.lang.Object