The Spring Framework

org.springframework.web.servlet.view
Class InternalResourceView

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
              extended by org.springframework.web.servlet.view.AbstractUrlBasedView
                  extended by org.springframework.web.servlet.view.InternalResourceView
All Implemented Interfaces:
BeanNameAware, InitializingBean, ApplicationContextAware, ServletContextAware, View
Direct Known Subclasses:
JstlView, TilesView

public class InternalResourceView
extends AbstractUrlBasedView

Wrapper for a JSP or other resource within the same web application. Exposes model objects as request attributes and forwards the request to the specified resource URL using a RequestDispatcher.

A URL for this view is supposed to specify a resource within the web application, suitable for RequestDispatcher's forward or include method.

If operating within an already included request or within a response that has already been committed, this view will fall back to an include instead of a forward. This can be enforced by calling response.flushBuffer() (which will commit the response) before rendering the view.

Typical usage with InternalResourceViewResolver would look as follows, from the perspective of the DispatcherServlet context definition:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/jsp/"/>
   <property name="suffix" value=".jsp"/>
 </bean>
Every view name returned from a handler will be translated to a JSP resource (for example: "myView" -> "/WEB-INF/jsp/myView.jsp"), using this view class by default.

Author:
Rod Johnson, Juergen Hoeller, Rob Harrop
See Also:
RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse), RequestDispatcher.include(javax.servlet.ServletRequest, javax.servlet.ServletResponse), ServletResponse.flushBuffer(), InternalResourceViewResolver

Field Summary
 
Fields inherited from class org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
InternalResourceView()
          Constructor for use as a bean.
InternalResourceView(String url)
          Create a new InternalResourceView with the given URL.
 
Method Summary
protected  void exposeForwardRequestAttributes(HttpServletRequest request)
          Expose the current request URI and paths as HttpServletRequest attributes under the keys defined in the Servlet 2.4 specification, for Servlet 2.3- containers.
protected  void exposeHelpers(HttpServletRequest request)
          Expose helpers unique to each rendering operation.
protected  String prepareForRendering(HttpServletRequest request, HttpServletResponse response)
          Prepare for rendering, and determine the request dispatcher path to forward to (or to include).
protected  void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
          Render the internal resource given the specified model.
protected  boolean useInclude(HttpServletRequest request, HttpServletResponse response)
          Determine whether to use RequestDispatcher's include or forward method.
 
Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView
afterPropertiesSet, getUrl, setUrl, toString
 
Methods inherited from class org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createRequestContext, exposeModelAsRequestAttributes, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute
 
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
 

Constructor Detail

InternalResourceView

public InternalResourceView()
Constructor for use as a bean.


InternalResourceView

public InternalResourceView(String url)
Create a new InternalResourceView with the given URL.

Parameters:
url - the URL to forward to
Method Detail

renderMergedOutputModel

protected void renderMergedOutputModel(Map model,
                                       HttpServletRequest request,
                                       HttpServletResponse response)
                                throws Exception
Render the internal resource given the specified model. This includes setting the model as request attributes.

Specified by:
renderMergedOutputModel in class AbstractView
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

exposeHelpers

protected void exposeHelpers(HttpServletRequest request)
                      throws Exception
Expose helpers unique to each rendering operation. This is necessary so that different rendering operations can't overwrite each other's contexts etc.

Called by renderMergedOutputModel(Map, HttpServletRequest, HttpServletResponse). The default implementation is empty. This method can be overridden to add custom helpers as request attributes.

Parameters:
request - current HTTP request
Throws:
Exception - if there's a fatal error while we're adding attributes
See Also:
renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse), JstlView.exposeHelpers(javax.servlet.http.HttpServletRequest), TilesJstlView.exposeHelpers(javax.servlet.http.HttpServletRequest)

prepareForRendering

protected String prepareForRendering(HttpServletRequest request,
                                     HttpServletResponse response)
                              throws Exception
Prepare for rendering, and determine the request dispatcher path to forward to (or to include).

This implementation simply returns the configured URL. Subclasses can override this to determine a resource to render, typically interpreting the URL in a different manner.

Parameters:
request - current HTTP request
response - current HTTP response
Returns:
the request dispatcher path to use
Throws:
Exception - if preparations failed
See Also:
AbstractUrlBasedView.getUrl(), TilesView.prepareForRendering(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

useInclude

protected boolean useInclude(HttpServletRequest request,
                             HttpServletResponse response)
Determine whether to use RequestDispatcher's include or forward method.

Performs a check whether an include URI attribute is found in the request, indicating an include request, and whether the response has already been committed. In both cases, an include will be performed, as a forward is not possible anymore.

Parameters:
request - current HTTP request
response - current HTTP response
Returns:
true for include, false for forward
See Also:
RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse), RequestDispatcher.include(javax.servlet.ServletRequest, javax.servlet.ServletResponse), ServletResponse.isCommitted(), WebUtils.isIncludeRequest(javax.servlet.ServletRequest)

exposeForwardRequestAttributes

protected void exposeForwardRequestAttributes(HttpServletRequest request)
Expose the current request URI and paths as HttpServletRequest attributes under the keys defined in the Servlet 2.4 specification, for Servlet 2.3- containers.

Does not override values if already present, to not conflict with Servlet 2.4+ containers.

See Also:
WebUtils.exposeForwardRequestAttributes(javax.servlet.http.HttpServletRequest)

The Spring Framework

Copyright © 2002-2006 The Spring Framework.