The Spring Framework

org.springframework.web.servlet.view
Class AbstractView

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.view.AbstractView
All Implemented Interfaces:
BeanNameAware, ApplicationContextAware, ServletContextAware, View
Direct Known Subclasses:
AbstractExcelView, AbstractJExcelView, AbstractPdfView, AbstractUrlBasedView, AbstractXsltView

public abstract class AbstractView
extends WebApplicationObjectSupport
implements View, BeanNameAware

Abstract base class for 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.

Author:
Rod Johnson, Juergen Hoeller
See Also:
setAttributes(java.util.Properties), setAttributesMap(java.util.Map), renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

Field Summary
static String DEFAULT_CONTENT_TYPE
          Default content type.
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
AbstractView()
           
 
Method Summary
 void addStaticAttribute(String name, Object value)
          Add static data to this view, exposed in each view.
protected  RequestContext createRequestContext(HttpServletRequest request, Map model)
          Create a RequestContext to expose under the specified attribute name.
protected  void exposeModelAsRequestAttributes(Map model, HttpServletRequest request)
          Expose the model objects in the given map as request attributes.
 Map getAttributesMap()
          Allow Map access to the static attributes of this view, with the option to add or override specific entries.
 String getBeanName()
          Return the view's name.
 String getContentType()
          Return the content type for this view.
 String getRequestContextAttribute()
          Return the name of the RequestContext attribute, if any.
 Map getStaticAttributes()
          Return the static attributes for this view.
 void render(Map 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(Map model, HttpServletRequest request, HttpServletResponse response)
          Subclasses must implement this method to actually render the view.
 void setAttributes(Properties attributes)
          Set static attributes for this view from a java.util.Properties object.
 void setAttributesCSV(String propString)
          Set static attributes as a CSV string.
 void setAttributesMap(Map attributes)
          Set static attributes for this view from a Map.
 void setBeanName(String beanName)
          Set the view's name.
 void setContentType(String contentType)
          Set the content type for this view.
 void setRequestContextAttribute(String requestContextAttribute)
          Set the name of the RequestContext attribute for this view.
 String toString()
           
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CONTENT_TYPE

public static final String DEFAULT_CONTENT_TYPE
Default content type. Overridable as bean property.

See Also:
Constant Field Values
Constructor Detail

AbstractView

public AbstractView()
Method Detail

setBeanName

public void setBeanName(String beanName)
Set the view's name. Helpful for traceability.

Framework code must call this when constructing views.

Specified by:
setBeanName in interface BeanNameAware
Parameters:
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.

getBeanName

public String getBeanName()
Return the view's name. Should never be null, if the view was correctly configured.


setContentType

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.


getContentType

public String getContentType()
Return the content type for this view.

Specified by:
getContentType in interface View
Returns:
the content type String (optionally including a character set), or null if not predetermined.

setRequestContextAttribute

public void setRequestContextAttribute(String requestContextAttribute)
Set the name of the RequestContext attribute for this view. Default is none.


getRequestContextAttribute

public String getRequestContextAttribute()
Return the name of the RequestContext attribute, if any.


setAttributesCSV

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.

Throws:
IllegalArgumentException

setAttributes

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:
PropertiesEditor

setAttributesMap

public void setAttributesMap(Map 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 with name Strings as keys and attribute objects as values

getAttributesMap

public Map 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.


addStaticAttribute

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 - the name of the attribute to expose
value - the attribute value to expose
See Also:
render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

getStaticAttributes

public Map 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:
the static attributes in this view

render

public void render(Map model,
                   HttpServletRequest request,
                   HttpServletResponse response)
            throws Exception
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.

Specified by:
render in interface View
Parameters:
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 request
response - HTTP response we are building
Throws:
Exception - if rendering failed
See Also:
renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

createRequestContext

protected RequestContext createRequestContext(HttpServletRequest request,
                                              Map model)
Create a RequestContext to expose under the specified attribute name.

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

Parameters:
request - current HTTP request
model - combined output Map (never null), with dynamic values taking precedence over static attributes
Returns:
the RequestContext instance
See Also:
setRequestContextAttribute(java.lang.String), RequestContext

renderMergedOutputModel

protected abstract void renderMergedOutputModel(Map 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 - combined output Map (never null), with dynamic values taking precedence over static attributes
request - current HTTP request
response - current HTTP response
Throws:
Exception - if rendering failed

exposeModelAsRequestAttributes

protected void exposeModelAsRequestAttributes(Map 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 RequestDispatcher.

Parameters:
model - Map of model objects to expose
request - current HTTP request
Throws:
Exception

toString

public String toString()
Overrides:
toString in class Object

The Spring Framework

Copyright © 2002-2007 The Spring Framework.