Class ServletWrappingController

              extended byorg.springframework.web.servlet.mvc.AbstractController
                  extended byorg.springframework.web.servlet.mvc.ServletWrappingController
All Implemented Interfaces:
ApplicationContextAware, BeanNameAware, Controller, DisposableBean, InitializingBean

public class ServletWrappingController
extends AbstractController
implements BeanNameAware, InitializingBean, DisposableBean

Spring Controller implementation that wraps a servlet instance which it manages internally. Such a wrapped servlet is not known outside of this controller; its entire lifecycle is covered here (in contrast to ServletForwardingController).

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 Struts actions in a Servlet 2.2 container. The Struts ActionServlet will be wrapped by this controller in such a scenario, rather than defined in web.xml. You then need to map "/*.do" (or whatever pattern you choose for your Struts actions) onto this controller, which will in turn forward to the Struts ActionServlet.

Note that Struts has a special requirement in that it parses web.xml to find its servlet mapping. Therefore, you need to specify the DispatcherServlet's servlet name as "servletName" on this controller, so that Struts finds the DispatcherServlet's mapping (thinking that it refers to the ActionServlet).

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: a DispatcherServlet XML context, forwarding "*.do" to the Struts ActionServlet wrapped by a ServletWrappingController. All such requests will go through the configured HandlerInterceptor chain (e.g. an OpenSessionInViewInterceptor). From the Struts 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="*.do">strutsWrappingController</prop>

 <bean id="strutsWrappingController" class="org.springframework.web.servlet.mvc.ServletWrappingController">
   <property name="servletClass">
   <property name="servletName">
   <property name="initParameters">
       <prop key="config">/WEB-INF/struts-config.xml</prop>
Thanks to Keith Garry Boyce for pointing out the issue with Struts in a Servlet 2.2 container, and for providing a prototype for accessing Struts through Spring's web dispatching infrastructure!

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

Field Summary
Fields inherited from class
Fields inherited from class
Constructor Summary
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
 void destroy()
          Invoked by a BeanFactory on destruction of a singleton.
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 setInitParameters(Properties initParameters)
          Specify init parameters for the servlet to wrap, as name-value pairs.
 void setServletClass(Class servletClass)
          Set the class of the servlet to wrap.
 void setServletName(String servletName)
          Set the name of the servlet to wrap.
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
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 ServletWrappingController()
Method Detail


public void setServletClass(Class servletClass)
Set the class of the servlet to wrap. Needs to implement javax.servlet.Servlet.

See Also:


public void setServletName(String servletName)
Set the name of the servlet to wrap. Default is the bean name of this controller.


public void setInitParameters(Properties initParameters)
Specify init parameters for the servlet to wrap, as name-value pairs.


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 like InitializingBean's afterPropertiesSet or a custom init-method.

Specified by:
setBeanName in interface BeanNameAware
name - the name of the bean in the factory


public void afterPropertiesSet()
                        throws Exception
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.


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)


public void destroy()
Description copied from interface: DisposableBean
Invoked by a BeanFactory on destruction of a singleton.

Specified by:
destroy in interface DisposableBean

Copyright (C) 2003-2004 The Spring Framework Project.