org.springframework.web.servlet.mvc
Class ServletWrappingController

java.lang.Object
  extended byorg.springframework.context.support.ApplicationObjectSupport
      extended byorg.springframework.web.context.support.WebApplicationObjectSupport
          extended byorg.springframework.web.servlet.support.WebContentGenerator
              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">
     <list>
       <ref bean="openSessionInViewInterceptor"/>
     </list>
   </property>
   <property name="mappings">
     <props>
       <prop key="*.do">strutsWrappingController</prop>
     </props>
   </property>
 </bean>

 <bean id="strutsWrappingController" class="org.springframework.web.servlet.mvc.ServletWrappingController">
   <property name="servletClass">
     <value>org.apache.struts.action.ActionServlet</value>
   </property>
   <property name="servletName">
     <value>action</value>
   </property>
   <property name="initParameters">
     <props>
       <prop key="config">/WEB-INF/struts-config.xml</prop>
     </props>
   </property>
 </bean>
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!

Since:
1.1.1
Author:
Juergen Hoeller
See Also:
ServletForwardingController, OpenSessionInViewInterceptor, OpenSessionInViewFilter, OpenPersistenceManagerInViewInterceptor, OpenPersistenceManagerInViewFilter

Field Summary
 
Fields inherited from class org.springframework.web.servlet.support.WebContentGenerator
HEADER_CACHE_CONTROL, HEADER_EXPIRES, HEADER_PRAGMA, METHOD_GET, METHOD_POST
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
ServletWrappingController()
           
 
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(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Template method.
 void setBeanName(java.lang.String name)
          Set the name of the bean in the bean factory that created this bean.
 void setInitParameters(java.util.Properties initParameters)
          Specify init parameters for the servlet to wrap, as name-value pairs.
 void setServletClass(java.lang.Class servletClass)
          Set the class of the servlet to wrap.
 void setServletName(java.lang.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 org.springframework.web.servlet.support.WebContentGenerator
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isRequireSession, isUseCacheControlHeader, isUseExpiresHeader, preventCaching, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseExpiresHeader
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
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

ServletWrappingController

public ServletWrappingController()
Method Detail

setServletClass

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

See Also:
Servlet

setServletName

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


setInitParameters

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


setBeanName

public void setBeanName(java.lang.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
Parameters:
name - the name of the bean in the factory

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.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
Throws:
java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

handleRequestInternal

protected ModelAndView handleRequestInternal(javax.servlet.http.HttpServletRequest request,
                                             javax.servlet.http.HttpServletResponse response)
                                      throws java.lang.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
Throws:
java.lang.Exception
See Also:
AbstractController.handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

destroy

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.