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 ApplicationObjectSupport, which will be helpful to some views. Handles static attributes, and merging static with dynamic attributes. Subclasses just need to implement the actual rendering.

It's recommended that subclasses don't cache anything, in the quest for efficiency. This class offers caching. However, it's possible to disable this class's caching, which is useful during development.

Version:
$Id: AbstractView.java,v 1.9 2004/03/18 02:46:11 trisberg Exp $
Author:
Rod Johnson, Juergen Hoeller
See Also:
renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

Field Summary
 
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.
protected  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 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, or null if not needed.
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, requiredContextClass
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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.

Returns:
the view's name

setContentType

public void setContentType(java.lang.String contentType)
Set the content type for this view. 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

getContentType

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

Returns:
content type for this view

setRequestContextAttribute

public void setRequestContextAttribute(java.lang.String requestContextAttribute)
Set the name of the RequestContext attribute for this view, or null if not needed.

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 of model attributes (model name String mapped to model object)
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 render the view.

The first take will be preparing the request: This may include setting the model elements as request attributes, e.g. in the case of a JSP view.

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.