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

public Class ServletForwardingController

extends AbstractController
implements BeanNameAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.Aware
Imports
jakarta.servlet.RequestDispatcher, .ServletContext, .ServletException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.beans.factory.BeanNameAware, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.servlet.ModelAndView, org.springframework.web.util.WebUtils

Spring Controller implementation that forwards to a named servlet, i.e. the "servlet-name" in web.xml rather than a URL path mapping. A target servlet doesn't even need a "servlet-mapping" in web.xml in the first place: A "servlet" declaration is sufficient.

Useful to invoke an existing servlet via Spring's dispatching infrastructure, for example to apply Spring HandlerInterceptors to its requests. This will work even in a minimal Servlet container that does not support Servlet filters.

Example: web.xml, mapping all "/myservlet" requests to a Spring dispatcher. Also defines a custom "myServlet", but without servlet mapping.

<servlet>
  <servlet-name>myServlet</servlet-name>
  <servlet-class>mypackage.TestServlet</servlet-class>
</servlet>

<servlet>
  <servlet-name>myDispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>myDispatcher</servlet-name>
  <url-pattern>/myservlet</url-pattern>
</servlet-mapping>
Example: myDispatcher-servlet.xml, in turn forwarding "/myservlet" to your servlet (identified by servlet name). All such requests will go through the configured HandlerInterceptor chain (e.g. an OpenSessionInViewInterceptor). From the servlet point of view, everything will work as usual.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="interceptors">
    <list>
      <ref bean="openSessionInViewInterceptor"/>
    </list>
  </property>
  <property name="mappings">
    <props>
      <prop key="/myservlet">myServletForwardingController</prop>
    </props>
  </property>
</bean>

<bean id="myServletForwardingController" class="org.springframework.web.servlet.mvc.ServletForwardingController">
  <property name="servletName"><value>myServlet</value></property>
</bean>
Author
Juergen Hoeller
Since
1.1.1
See Also
ServletWrappingController, org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor, org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter

Field Summary

Modifier and TypeField and Description
private String
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected ModelAndView
public void
setBeanName(String
the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.
name
)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

Set the name of the bean in the bean factory that created this bean.

public void
setServletName(String servletName)

Set the name of the servlet to forward to, i.e. the "servlet-name" of the target servlet in web.xml.

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.mvc.AbstractController:
handleRequestisSynchronizeOnSessionsetSynchronizeOnSession

Field Detail

beanNameback to summary
private String beanName
Annotations
@Nullable
servletNameback to summary
private String servletName
Annotations
@Nullable

Constructor Detail

ServletForwardingControllerback to summary
public ServletForwardingController()

Method Detail

handleRequestInternalback to summary
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception

Implements abstract org.springframework.web.servlet.mvc.AbstractController.handleRequestInternal.

Doc from org.springframework.web.servlet.mvc.AbstractController.handleRequestInternal.

Template method. Subclasses must implement this. The contract is the same as for handleRequest.

Annotations
@Override
setBeanNameback to summary
public void setBeanName(String name)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

Doc from org.springframework.beans.factory.BeanNameAware.setBeanName.

Set the name of the bean in the bean factory that created this bean.

Invoked after population of normal bean properties but before an init callback such as InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
name:String

the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.

Annotations
@Override
setServletNameback to summary
public void setServletName(String servletName)

Set the name of the servlet to forward to, i.e. the "servlet-name" of the target servlet in web.xml.

Default is the bean name of this controller.

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