org.springframework.web.servlet.view
Class AbstractView

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

public abstract class AbstractView
extends WebApplicationObjectSupport
implements View, BeanNameAware

Abstract View superclass. Standard framework View implementations and application-specific custom Views can extend this class to simplify their implementation. Subclasses should be JavaBeans.

Extends WebApplicationObjectSupport, which will be helpful to some views. Handles static attributes, and merging static with dynamic attributes. Subclasses just need to implement the actual rendering.

Version:
$Id: AbstractView.java,v 1.10 2004/05/04 08:39:31 jhoeller Exp $
Author:
Rod Johnson, Juergen Hoeller
See Also:
renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

Field Summary
static java.lang.String DEFAULT_CONTENT_TYPE
          Default content type.
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
AbstractView()
           
 
Method Summary
 void addStaticAttribute(java.lang.String name, java.lang.Object value)
          Add static data to this view, exposed in each view.
 java.lang.String getBeanName()
          Return the view's name.
 java.lang.String getContentType()
          Return the content type for this view.
 java.util.Map getStaticAttributes()
          Handy for testing.
 void render(java.util.Map model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.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 model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Subclasses must implement this method to actually render the view.
 void setAttributes(java.util.Properties props)
          Set static attributes from a java.util.Properties object.
 void setAttributesCSV(java.lang.String propString)
          Set static attributes as a CSV string.
 void setAttributesMap(java.util.Map attributes)
          Set static attributes 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 setRequestContextAttribute(java.lang.String requestContextAttribute)
          Set the name of the RequestContext attribute for this view.
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext
 
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, toString, wait, wait, wait
 

Field Detail

DEFAULT_CONTENT_TYPE

public static final java.lang.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(java.lang.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 view's name. May not be null. Views should use this for log messages.

getBeanName

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


setContentType

public void setContentType(java.lang.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.

Parameters:
contentType - content type for this view
See Also:
DEFAULT_CONTENT_TYPE

getContentType

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


setRequestContextAttribute

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

Parameters:
requestContextAttribute - name of the RequestContext attribute

setAttributes

public void setAttributes(java.util.Properties props)
Set static attributes from a java.util.Properties object. 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(java.util.Map attributes)
Set static attributes from a Map. This allows to set any kind of attribute values, for example bean references.

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

setAttributesCSV

public void setAttributesCSV(java.lang.String propString)
                      throws java.lang.IllegalArgumentException
Set static attributes as a CSV string. Format is: attname0={value1},attname1={value1}

Throws:
java.lang.IllegalArgumentException

addStaticAttribute

public void addStaticAttribute(java.lang.String name,
                               java.lang.Object value)
Add static data to this view, exposed in each view.

Must be invoked before any calls to render().

Parameters:
name - name of attribute to expose
value - object to expose

getStaticAttributes

public java.util.Map getStaticAttributes()
Handy for testing. Return the static attributes held in this view.

Returns:
the static attributes in this view

render

public void render(java.util.Map model,
                   javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws java.lang.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:
java.lang.Exception - if rendering failed
See Also:
renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

renderMergedOutputModel

protected abstract void renderMergedOutputModel(java.util.Map model,
                                                javax.servlet.http.HttpServletRequest request,
                                                javax.servlet.http.HttpServletResponse response)
                                         throws java.lang.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, with dynamic values taking precedence over static attributes
request - current HTTP request
response - current HTTP response
Throws:
java.lang.Exception - if rendering failed


Copyright (C) 2003-2004 The Spring Framework Project.