站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 2.0.5 API 文档英文版

ServletWrappingController (Spring Framework API 2.0) - Spring Framework 2.0.5 API 文档英文版

The Spring Framework

org.springframework.web.servlet.mvc
Class ServletWrappingController

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.support.WebContentGenerator
              extended by org.springframework.web.servlet.mvc.AbstractController
                  extended by org.springframework.web.servlet.mvc.ServletWrappingController
All Implemented Interfaces:
BeanNameAware, DisposableBean, InitializingBean, ApplicationContextAware, ServletContextAware, Controller

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
METHOD_GET, METHOD_HEAD, METHOD_POST
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
ServletWrappingController()
           
 
Method Summary
 void afterPropertiesSet()
          Initialize the wrapped Servlet instance.
 void destroy()
          Destroy the wrapped Servlet instance.
protected  ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
          Invoke the the wrapped Servlet instance.
 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 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, isContextRequired, setServletContext
 
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(Class servletClass)
Set the class of the servlet to wrap. Needs to implement javax.servlet.Servlet.

See Also:
Servlet

setServletName

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


setInitParameters

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


setBeanName

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
Parameters:
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.

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Initialize the wrapped Servlet instance.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.
See Also:
Servlet.init(javax.servlet.ServletConfig)

handleRequestInternal

protected ModelAndView handleRequestInternal(HttpServletRequest request,
                                             HttpServletResponse response)
                                      throws Exception
Invoke the the wrapped Servlet instance.

Specified by:
handleRequestInternal in class AbstractController
Throws:
Exception
See Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

destroy

public void destroy()
Destroy the wrapped Servlet instance.

Specified by:
destroy in interface DisposableBean
See Also:
Servlet.destroy()

The Spring Framework

Copyright © 2002-2007 The Spring Framework.