org.springframework.web.struts
Class ContextLoaderPlugIn


public class ContextLoaderPlugIn
implements PlugIn

Struts 1.1 PlugIn that loads a Spring application context for the Struts ActionServlet. This context will automatically refer to the root WebApplicationContext (loaded by ContextLoaderListener/Servlet) as parent.

The default namespace of the WebApplicationContext is the name of the Struts ActionServlet, suffixed with "-servlet" (e.g. "action-servlet"). The default location of the XmlWebApplicationContext configuration file is therefore "/WEB-INF/action-servlet.xml".

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>
The location of the context configuration files can be customized through the "contextConfigLocation" setting, analogous to the root WebApplicationContext and FrameworkServlet contexts.
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml /WEB-INF/myContext.xml"/>
</plug-in>
Beans defined in the ContextLoaderPlugin context can be accessed from conventional Struts Actions, via fetching the WebApplicationContext reference from the ServletContext. ActionSupport and DispatchActionSupport are pre-built convenience classes that provide easy access to the context.

It is normally preferable to access Spring's root WebApplicationContext in such scenarios, though: A shared middle tier should be defined there rather than in a ContextLoaderPlugin context, for access by any web component. ActionSupport and DispatchActionSupport auto-detect the root context too.

A special usage of this PlugIn is to define Struts Actions themselves as beans, typically wiring them with middle tier components defined in the root context. Such Actions will then be delegated to by proxy definitions in the Struts configuration, using the DelegatingActionProxy class or the DelegatingRequestProcessor.

Note that you can use a single ContextLoaderPlugIn for all Struts modules. That context can in turn be loaded from multiple XML files, for example split according to Struts modules. Alternatively, define one ContextLoaderPlugIn per Struts module, specifying appropriate "contextConfigLocation" parameters.

Note: The idea of delegating to Spring-managed Struts Actions originated in Don Brown's Spring Struts Plugin. ContextLoaderPlugIn and DelegatingActionProxy constitute a clean-room implementation of the same idea, essentially superseding the original plugin. Many thanks to Don Brown and Matt Raible for the original work, and for the agreement to reimplement the idea in standard Spring!

Author:
Juergen Hoeller
Since: 1.0.1
See Also: SERVLET_CONTEXT_PREFIX, ActionSupport, DispatchActionSupport, DelegatingActionProxy, DelegatingRequestProcessor, DelegatingTilesRequestProcessor, ContextLoaderListener, ContextLoaderServlet, FrameworkServlet

Field Summary
 final static ClassDEFAULT_CONTEXT_CLASS
          Default context class for ContextLoaderPlugIn.
 final static StringDEFAULT_NAMESPACE_SUFFIX
          Suffix for WebApplicationContext namespaces.
 final static StringSERVLET_CONTEXT_PREFIX
          Prefix for the ServletContext attribute for the WebApplicationContext.

Method Summary
 voiddestroy()
          Close the WebApplicationContext of the ActionServlet.
 final ActionServletgetActionServlet()
          Return the Struts ActionServlet that this PlugIn is associated with.
 ClassgetContextClass()
          Return the custom context class.
 StringgetContextConfigLocation()
          Return the explicit context config location, if any.
 final ModuleConfiggetModuleConfig()
          Return the Struts ModuleConfig that this PlugIn is associated with.
 final StringgetModulePrefix()
          Return the prefix of the ModuleConfig that this PlugIn is associated with.
 StringgetNamespace()
          Return the namespace for the ActionServlet, falling back to default scheme if no custom namespace was set: e.g.
 final ServletContextgetServletContext()
          Return the ServletContext that this PlugIn is associated with.
 StringgetServletContextAttributeName()
          Return the ServletContext attribute name for this PlugIn's WebApplicationContext.
 final StringgetServletName()
          Return the name of the ActionServlet that this PlugIn is associated with.
 final WebApplicationContextgetWebApplicationContext()
          Return this PlugIn's WebApplicationContext.
 final voidinit(ActionServlet actionServlet, ModuleConfig moduleConfig)
          Create the ActionServlet's WebApplicationContext.
 voidsetContextClass(Class contextClass)
          Set a custom context class.
 voidsetContextClassName(String contextClassName)
          Set a custom context class by name.
 voidsetContextConfigLocation(String contextConfigLocation)
          Set the context config location explicitly, instead of relying on the default location built from the namespace.
 voidsetNamespace(String namespace)
          Set a custom namespace for the ActionServlet, to be used for building a default context config location.

Field Detail

DEFAULT_CONTEXT_CLASS

public final static Class DEFAULT_CONTEXT_CLASS
Default context class for ContextLoaderPlugIn.
See Also:
XmlWebApplicationContext

DEFAULT_NAMESPACE_SUFFIX

public final static String DEFAULT_NAMESPACE_SUFFIX
Suffix for WebApplicationContext namespaces. If a Struts ActionServlet is given the name "action" in a context, the namespace used by this PlugIn will resolve to "action-servlet".

SERVLET_CONTEXT_PREFIX

public final static String SERVLET_CONTEXT_PREFIX
Prefix for the ServletContext attribute for the WebApplicationContext. The completion is the Struts module name.
Method Detail

destroy

public void destroy()
Close the WebApplicationContext of the ActionServlet.
See Also:
org.springframework.context.ConfigurableApplicationContext.close()

getActionServlet

public final ActionServlet getActionServlet()
Return the Struts ActionServlet that this PlugIn is associated with.

getContextClass

public Class getContextClass()
Return the custom context class.

getContextConfigLocation

public String getContextConfigLocation()
Return the explicit context config location, if any.

getModuleConfig

public final ModuleConfig getModuleConfig()
Return the Struts ModuleConfig that this PlugIn is associated with.

getModulePrefix

public final String getModulePrefix()
Return the prefix of the ModuleConfig that this PlugIn is associated with.
See Also:
org.apache.struts.config.ModuleConfig#getPrefix

getNamespace

public String getNamespace()
Return the namespace for the ActionServlet, falling back to default scheme if no custom namespace was set: e.g. "test-servlet" for a servlet named "test".

getServletContext

public final ServletContext getServletContext()
Return the ServletContext that this PlugIn is associated with.

getServletContextAttributeName

public String getServletContextAttributeName()
Return the ServletContext attribute name for this PlugIn's WebApplicationContext. Default implementation returns SERVLET_CONTEXT_PREFIX + module prefix.
See Also:
SERVLET_CONTEXT_PREFIX, getModulePrefix()

getServletName

public final String getServletName()
Return the name of the ActionServlet that this PlugIn is associated with.

getWebApplicationContext

public final WebApplicationContext getWebApplicationContext()
Return this PlugIn's WebApplicationContext.

init

public final void init(ActionServlet actionServlet, ModuleConfig moduleConfig)
throws ServletException
Create the ActionServlet's WebApplicationContext.

setContextClass

public void setContextClass(Class contextClass)
Set a custom context class. This class must be of type WebApplicationContext, when using the default ContextLoaderPlugIn implementation, the context class must also implement ConfigurableWebApplicationContext.
See Also:
createWebApplicationContext(org.springframework.web.context.WebApplicationContext)

setContextClassName

public void setContextClassName(String contextClassName)
throws java.lang.ClassNotFoundException
Set a custom context class by name. This class must be of type WebApplicationContext, when using the default ContextLoaderPlugIn implementation, the context class must also implement ConfigurableWebApplicationContext.
See Also:
createWebApplicationContext(org.springframework.web.context.WebApplicationContext)

setContextConfigLocation

public void setContextConfigLocation(String contextConfigLocation)
Set the context config location explicitly, instead of relying on the default location built from the namespace. This location string can consist of multiple locations separated by any number of commas and spaces.

setNamespace

public void setNamespace(String namespace)
Set a custom namespace for the ActionServlet, to be used for building a default context config location.

Association Links

to Class java.lang.String

Suffix for WebApplicationContext namespaces. If a Struts ActionServlet is given the name "action" in a context, the namespace used by this PlugIn will resolve to "action-servlet".

to Class java.lang.Class

Default context class for ContextLoaderPlugIn.

see org.springframework.web.context.support.XmlWebApplicationContext

to Class java.lang.String

Prefix for the ServletContext attribute for the WebApplicationContext. The completion is the Struts module name.

to Class java.lang.Class

Custom WebApplicationContext class

to Class java.lang.String

Namespace for this servlet

to Class java.lang.String

Explicit context config location

to Class org.springframework.web.context.WebApplicationContext

WebApplicationContext for the ActionServlet