public class InternalResourceView extends AbstractUrlBasedView
javax.servlet.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.
javax.servlet.RequestDispatcher#forward
,
javax.servlet.RequestDispatcher#include
,
javax.servlet.ServletResponse#flushBuffer
,
InternalResourceViewResolver
,
JstlView
Modifier and Type | Field and Description |
---|---|
private boolean |
alwaysInclude |
private boolean |
preventDispatchLoop |
DEFAULT_CONTENT_TYPE
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
InternalResourceView()
Constructor for use as a bean.
|
InternalResourceView(java.lang.String url)
Create a new InternalResourceView with the given URL.
|
InternalResourceView(java.lang.String url,
boolean alwaysInclude)
Create a new InternalResourceView with the given URL.
|
Modifier and Type | Method and Description |
---|---|
protected void |
exposeHelpers(HttpServletRequest request)
Expose helpers unique to each rendering operation.
|
protected RequestDispatcher |
getRequestDispatcher(HttpServletRequest request,
java.lang.String path)
Obtain the RequestDispatcher to use for the forward/include.
|
protected boolean |
isContextRequired()
An ApplicationContext is not strictly required for InternalResourceView.
|
protected java.lang.String |
prepareForRendering(HttpServletRequest request,
HttpServletResponse response)
Prepare for rendering, and determine the request dispatcher path
to forward to (or to include).
|
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> 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 |
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. |
afterPropertiesSet, checkResource, getUrl, isUrlRequired, setUrl, toString
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
private boolean alwaysInclude
private boolean preventDispatchLoop
public InternalResourceView()
public InternalResourceView(java.lang.String url)
url
- the URL to forward tosetAlwaysInclude(boolean)
public InternalResourceView(java.lang.String url, boolean alwaysInclude)
url
- the URL to forward toalwaysInclude
- whether to always include the view rather than forward to itpublic void setAlwaysInclude(boolean alwaysInclude)
Default is "false". Switch this flag on to enforce the use of a Servlet include, even if a forward would be possible.
javax.servlet.RequestDispatcher#forward
,
javax.servlet.RequestDispatcher#include
,
#useInclude(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
public void setPreventDispatchLoop(boolean preventDispatchLoop)
Default is "false". Switch this to "true" for convention-based views where a dispatch back to the current handler path is a definitive error.
protected boolean isContextRequired()
isContextRequired
in class WebApplicationObjectSupport
ApplicationObjectSupport.getApplicationContext()
,
ApplicationObjectSupport.getMessageSourceAccessor()
,
WebApplicationObjectSupport.getWebApplicationContext()
,
WebApplicationObjectSupport.getServletContext()
,
WebApplicationObjectSupport.getTempDir()
protected void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
renderMergedOutputModel
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributesrequest
- current HTTP requestresponse
- current HTTP responsejava.lang.Exception
- if rendering failedprotected void exposeHelpers(HttpServletRequest request) throws java.lang.Exception
Called by renderMergedOutputModel(Map, HttpServletRequest, HttpServletResponse)
.
The default implementation is empty. This method can be overridden to add
custom helpers as request attributes.
request
- current HTTP requestjava.lang.Exception
- if there's a fatal error while we're adding attributesrenderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, HttpServletRequest, HttpServletResponse)
,
JstlView.exposeHelpers(HttpServletRequest)
protected java.lang.String prepareForRendering(HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
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.
request
- current HTTP requestresponse
- current HTTP responsejava.lang.Exception
- if preparations failedAbstractUrlBasedView.getUrl()
protected RequestDispatcher getRequestDispatcher(HttpServletRequest request, java.lang.String path)
The default implementation simply calls
HttpServletRequest#getRequestDispatcher(String)
.
Can be overridden in subclasses.
request
- current HTTP requestpath
- the target URL (as returned from prepareForRendering(HttpServletRequest, HttpServletResponse)
)protected boolean useInclude(HttpServletRequest request, HttpServletResponse response)
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.
request
- current HTTP requestresponse
- current HTTP responsetrue
for include, false
for forwardjavax.servlet.RequestDispatcher#forward
,
javax.servlet.RequestDispatcher#include
,
javax.servlet.ServletResponse#isCommitted
,
WebUtils.isIncludeRequest(ServletRequest)