The Spring Framework

Class ServletForwardingController

  extended by
      extended by
          extended by
              extended by org.springframework.web.servlet.mvc.AbstractController
                  extended by org.springframework.web.servlet.mvc.ServletForwardingController
All Implemented Interfaces:
BeanNameAware, ApplicationContextAware, ServletContextAware, Controller

public class ServletForwardingController
extends AbstractController
implements BeanNameAware

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 Servlet 2.2 container that does not support Servlet filters.

In particular, the main intent of this controller is to allow for applying Spring's OpenSessionInViewInterceptor or OpenPersistenceManagerInViewInterceptor to servlets in a Servlet 2.2 container. The specified "servlet-name" will simply refer to a custom servlet definition in web.xml in such a scenario. You then need to map "/myservlet" (or whatever path you choose for your servlet) onto this controller, which will in turn forward to your servlet.

In a Servlet 2.3 container, when not using Spring's own web MVC framework, it is recommended to use classic servlet mapping in combination with a filter, for example Spring's OpenSessionInViewFilter or OpenPersistenceManagerInViewFilter.

Example: web.xml, mapping all "/myservlet" requests to a Spring dispatcher. Also defines a custom "myServlet", but without 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">
       <ref bean="openSessionInViewInterceptor"/>
   <property name="mappings">
       <prop key="/myservlet">myServletForwardingController</prop>

 <bean id="myServletForwardingController" class="org.springframework.web.servlet.mvc.ServletForwardingController">
   <property name="servletName"><value>myServlet</value></property>

Juergen Hoeller
See Also:
ServletWrappingController, OpenSessionInViewInterceptor, OpenSessionInViewFilter, OpenPersistenceManagerInViewInterceptor, OpenPersistenceManagerInViewFilter

Field Summary
Fields inherited from class
Fields inherited from class
Constructor Summary
Method Summary
protected  ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
          Template method.
 void setBeanName(String name)
          Set the name of the bean in the bean factory that created this bean.
 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 useInclude(HttpServletRequest request, HttpServletResponse response)
          Determine whether to use RequestDispatcher's include or forward method.
Methods inherited from class org.springframework.web.servlet.mvc.AbstractController
handleRequest, isSynchronizeOnSession, setSynchronizeOnSession
Methods inherited from class
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isRequireSession, isUseCacheControlHeader, isUseExpiresHeader, preventCaching, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseExpiresHeader
Methods inherited from class
getServletContext, getTempDir, getWebApplicationContext, isContextRequired, setServletContext
Methods inherited from class
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public ServletForwardingController()
Method Detail


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.


public void setBeanName(String name)
Description copied from interface: BeanNameAware
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.

Specified by:
setBeanName in interface BeanNameAware
name - 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.


protected ModelAndView handleRequestInternal(HttpServletRequest request,
                                             HttpServletResponse response)
                                      throws Exception
Description copied from class: AbstractController
Template method. Subclasses must implement this. The contract is the same as for handleRequest.

Specified by:
handleRequestInternal in class AbstractController
See Also:
AbstractController.handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)


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.

request - current HTTP request
response - current HTTP response
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)

The Spring Framework

Copyright © 2002-2007 The Spring Framework.