Package org.springframework.web.servlet.view

# Class InternalResourceView

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.AbstractUrlBasedView
org.springframework.web.servlet.view.InternalResourceView

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.ApplicationContextAware`, `org.springframework.web.context.ServletContextAware`, `View`

Direct Known Subclasses:
  `JstlView`

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(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)`
  - `RequestDispatcher.include(jakarta.servlet.ServletRequest, jakarta.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`

### Fields inherited from interface org.springframework.web.servlet.View

`PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE`

## Constructor Summary

Constructors
Constructor
Description
`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

Modifier and Type
Method
Description
`protected void`
`exposeHelpers(HttpServletRequest request)`
Expose helpers unique to each rendering operation.
`protected RequestDispatcher`
`getRequestDispatcher(HttpServletRequest request, String path)`
Obtain the RequestDispatcher to use for the forward/include.
`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<String,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.

### Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView

`afterPropertiesSet, checkResource, getUrl, isUrlRequired, setUrl, toString`

### Methods inherited from class org.springframework.web.servlet.view.AbstractView

`addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse`

### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

`getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, setServletContext`

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### 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 Details

### 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(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)`
  - `RequestDispatcher.include(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)`
  - `useInclude(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)`

### 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 `org.springframework.web.context.support.WebApplicationObjectSupport`

### renderMergedOutputModel

protected void renderMergedOutputModel(Map<String,Object> 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<java.lang.String, java.lang.Object>, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)`
  - `JstlView.exposeHelpers(jakarta.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()`

### getRequestDispatcher

@Nullable protected RequestDispatcher getRequestDispatcher(HttpServletRequest request, String path)
Obtain the RequestDispatcher to use for the forward/include.

The default implementation simply calls `ServletRequest.getRequestDispatcher(String)`. Can be overridden in subclasses.

Parameters:
  `request` - current HTTP request
  `path` - the target URL (as returned from `prepareForRendering(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)`)
Returns:
  a corresponding RequestDispatcher

### 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(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)`
  - `RequestDispatcher.include(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)`
  - `ServletResponse.isCommitted()`
  - `WebUtils.isIncludeRequest(jakarta.servlet.ServletRequest)`