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 looks 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, JstlView

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.
InternalResourceView(String url, boolean alwaysInclude)
          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 as well as misbehaving Servlet 2.4 containers (such as OC4J).
protected  void exposeHelpers(HttpServletRequest request)
          Expose helpers unique to each rendering operation.
protected  HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest)
          Get the request handle to expose to the RequestDispatcher, i.e. to the view.
protected  void initServletContext(ServletContext sc)
          Checks whether we need explictly expose the Servlet 2.4 request attributes by default.
protected  boolean isContextRequired()
          An ApplicationContext is not strictly required for InternalResourceView.
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.
 void setAlwaysInclude(boolean alwaysInclude)
          Specify whether to always include the view rather than forward to it.
 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(String[] exposedContextBeanNames)
          Specify the names of beans in the context which are supposed to be exposed.
 void setExposeForwardAttributes(boolean exposeForwardAttributes)
          Set whether to explictly expose the Servlet 2.4 forward request attributes when forwarding to the underlying view resource.
 void setPreventDispatchLoop(boolean preventDispatchLoop)
          Set whether to explicitly prevent dispatching back to the current handler path.
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, isUrlRequired, setUrl, toString
 
Methods inherited from class org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createRequestContext, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, 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.

See Also:
AbstractUrlBasedView.setUrl(java.lang.String), setAlwaysInclude(boolean)

InternalResourceView

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

Parameters:
url - the URL to forward to
See Also:
setAlwaysInclude(boolean)

InternalResourceView

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

Parameters:
url - the URL to forward to
alwaysInclude - whether to always include the view rather than forward to it
Method Detail

setAlwaysInclude

public void setAlwaysInclude(boolean alwaysInclude)
Specify whether to always include the view rather than forward to it.

Default is "false". Switch this flag on to enforce the use of a Servlet include, even if a forward would be possible.

See Also:
RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse), RequestDispatcher.include(javax.servlet.ServletRequest, javax.servlet.ServletResponse), useInclude(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

setExposeForwardAttributes

public void setExposeForwardAttributes(boolean exposeForwardAttributes)
Set whether to explictly expose the Servlet 2.4 forward request attributes when forwarding to the underlying view resource.

Default is "true" on Servlet containers up until 2.4, and "false" for Servlet 2.5 and above. Note that Servlet containers at 2.4 level and above should expose those attributes automatically! This InternalResourceView feature exists for Servlet 2.3 containers and misbehaving 2.4 containers.


setExposeContextBeansAsAttributes

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

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.

See Also:
getRequestToExpose(javax.servlet.http.HttpServletRequest)

setExposedContextBeanNames

public void setExposedContextBeanNames(String[] exposedContextBeanNames)
Specify the names of beans in the context which are supposed to be exposed. If this is non-null, only the specified beans are eligible for exposure as attributes.

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.


setPreventDispatchLoop

public void setPreventDispatchLoop(boolean preventDispatchLoop)
Set whether to explicitly prevent dispatching back to the current handler path.

Default is "false". Switch this to "true" for convention-based views where a dispatch back to the current handler path is a definitive error.


isContextRequired

protected boolean isContextRequired()
An ApplicationContext is not strictly required for InternalResourceView.

Overrides:
isContextRequired in class WebApplicationObjectSupport
See Also:
ApplicationObjectSupport.getApplicationContext(), ApplicationObjectSupport.getMessageSourceAccessor(), WebApplicationObjectSupport.getWebApplicationContext(), WebApplicationObjectSupport.getServletContext(), WebApplicationObjectSupport.getTempDir()

initServletContext

protected void initServletContext(ServletContext sc)
Checks whether we need explictly expose the Servlet 2.4 request attributes by default.

Overrides:
initServletContext in class WebApplicationObjectSupport
Parameters:
sc - the ServletContext that this application object runs in (never null)
See Also:
setExposeForwardAttributes(boolean), exposeForwardRequestAttributes(javax.servlet.http.HttpServletRequest)

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)

getRequestToExpose

protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest)
Get the request handle to expose to the RequestDispatcher, i.e. to the view.

The default implementation wraps the original request for exposure of Spring beans as request attributes (if demanded).

Parameters:
originalRequest - the original servlet request as provided by the engine
Returns:
the wrapped request, or the original request if no wrapping is necessary
See Also:
setExposeContextBeansAsAttributes(boolean), ContextExposingHttpServletRequest

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 as well as misbehaving Servlet 2.4 containers (such as OC4J).

Does not expose the attributes on Servlet 2.5 or above, mainly for GlassFish compatibility (GlassFish gets confused by pre-exposed attributes). In any case, Servlet 2.5 containers should finally properly support Servlet 2.4 features, shouldn't they...

Parameters:
request - current HTTP request
See Also:
WebUtils.exposeForwardRequestAttributes(javax.servlet.http.HttpServletRequest)

The Spring Framework

Copyright © 2002-2008 The Spring Framework.