Top Description Fields Constructors Methods
org.springframework.web.servlet.view

public Class InternalResourceView

extends AbstractUrlBasedView
Class Inheritance
Known Direct Subclasses
org.springframework.web.servlet.view.JstlView
Imports
java.util.Map, jakarta.servlet.RequestDispatcher, .ServletException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils, org.springframework.web.util.WebUtils

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 jakarta.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.
Authors
Rod Johnson, Juergen Hoeller, Rob Harrop
See Also
jakarta.servlet.RequestDispatcher#forward, jakarta.servlet.RequestDispatcher#include, jakarta.servlet.ServletResponse#flushBuffer, InternalResourceViewResolver, JstlView

Field Summary

Modifier and TypeField and Description
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
InternalResourceView()

Constructor for use as a bean.

public
InternalResourceView(String
the URL to forward to
url
)

Create a new InternalResourceView with the given URL.

public
InternalResourceView(String
the URL to forward to
url
,
boolean
whether to always include the view rather than forward to it
alwaysInclude
)

Create a new InternalResourceView with the given URL.

Method Summary

Modifier and TypeMethod and Description
protected void
exposeHelpers(HttpServletRequest
current HTTP request
request
)

Expose helpers unique to each rendering operation.

protected RequestDispatcher

Returns:

a corresponding RequestDispatcher
getRequestDispatcher
(HttpServletRequest
current HTTP request
request
,
String
the target URL (as returned from prepareForRendering)
path
)

Obtain the RequestDispatcher to use for the forward/include.

protected boolean
isContextRequired()

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.isContextRequired.

An ApplicationContext is not strictly required for InternalResourceView.

protected String

Returns:

the request dispatcher path to use
prepareForRendering
(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Prepare for rendering, and determine the request dispatcher path to forward to (or to include).

protected void
renderMergedOutputModel(Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Render the internal resource given the specified model.

public void
setAlwaysInclude(boolean alwaysInclude)

Specify whether to always include the view rather than forward to it.

public void
setPreventDispatchLoop(boolean preventDispatchLoop)

Set whether to explicitly prevent dispatching back to the current handler path.

protected boolean

Returns:

true for include, false for forward
useInclude
(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Determine whether to use RequestDispatcher's include or forward method.

Inherited from org.springframework.web.servlet.view.AbstractUrlBasedView:
afterPropertiesSetcheckResourcegetUrlisUrlRequiredsetUrltoString

Field Detail

alwaysIncludeback to summary
private boolean alwaysInclude
preventDispatchLoopback to summary
private boolean preventDispatchLoop

Constructor Detail

InternalResourceViewback to summary
public InternalResourceView()

Constructor for use as a bean.

See Also
setUrl, setAlwaysInclude
InternalResourceViewback to summary
public InternalResourceView(String url)

Create a new InternalResourceView with the given URL.

Parameters
url:String

the URL to forward to

See Also
setAlwaysInclude
InternalResourceViewback to summary
public InternalResourceView(String url, boolean alwaysInclude)

Create a new InternalResourceView with the given URL.

Parameters
url:String

the URL to forward to

alwaysInclude:boolean

whether to always include the view rather than forward to it

Method Detail

exposeHelpersback to summary
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:HttpServletRequest

current HTTP request

Exceptions
Exception:
if there's a fatal error while we're adding attributes
See Also
renderMergedOutputModel, JstlView#exposeHelpers
getRequestDispatcherback to summary
protected RequestDispatcher getRequestDispatcher(HttpServletRequest request, String path)

Obtain the RequestDispatcher to use for the forward/include.

The default implementation simply calls HttpServletRequest#getRequestDispatcher(String). Can be overridden in subclasses.

Parameters
request:HttpServletRequest

current HTTP request

path:String

the target URL (as returned from prepareForRendering)

Returns:RequestDispatcher

a corresponding RequestDispatcher

Annotations
@Nullable
isContextRequiredback to summary
protected boolean isContextRequired()

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.isContextRequired.

An ApplicationContext is not strictly required for InternalResourceView.

Annotations
@Override
prepareForRenderingback to summary
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:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

Returns:String

the request dispatcher path to use

Exceptions
Exception:
if preparations failed
See Also
getUrl()
renderMergedOutputModelback to summary
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Render the internal resource given the specified model. This includes setting the model as request attributes.

Parameters
model:Map<String, Object>

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

combined output Map (never null), with dynamic values taking precedence over static attributes

request:HttpServletRequest

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

current HTTP response

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

if rendering failed

setAlwaysIncludeback to summary
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
jakarta.servlet.RequestDispatcher#forward, jakarta.servlet.RequestDispatcher#include, useInclude(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)
setPreventDispatchLoopback to summary
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.

useIncludeback to summary
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:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

Returns:boolean

true for include, false for forward

See Also
jakarta.servlet.RequestDispatcher#forward, jakarta.servlet.RequestDispatcher#include, jakarta.servlet.ServletResponse#isCommitted, org.springframework.web.util.WebUtils#isIncludeRequest