Step 1:
Load the spring context file using the ContextLoader Listener in the web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
Usages:
Usage 1: Using the loaded beans in the Action Forms:
We can use the loaded beans from the servletContext using the WebApplicationContextUtils provided by spring.
Example:
import org.springframework.web.context.support.WebApplicationContextUtils;
WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext()).getBean("TestService");
Usage 2: Using the loaded beans in Action Classes:
Another convienient way of using the loaded beans in the Action Class
extend Action class with org.springframework.web.struts.ActionSupport
and use the getWebApplicationContext().getBean("testService");
Example:
public class TestAction extends ActionSupport
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
TestService serv = (TestService) getWebApplicationContext().getBean("testService");
}
}
Usage 3: Using Spring-managed Struts Action , so that all the beans gets injected directly into the Struts.
Proxy for a Spring-managed Struts Action that's defined in ContextLoaderPlugIn's WebApplicationContext.
The proxy is defined in the Struts config file, specifying this class as action class. It will delegate to a Struts Action bean in the ContextLoaderPlugIn context.
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"/>
The name of the Action bean in the WebApplicationContext will be determined from the mapping path and module prefix. This can be customized by overriding the determineActionBeanName method.
mapping path "/login" -> bean name "/login"
mapping path "/login", module prefix "/mymodule" -> bean name "/mymodule/login"
A corresponding bean definition in the ContextLoaderPlugin context looks as follows, being able to fully leverage Spring's configuration facilities:
<bean class="myapp.MyAction" name="/login">
<property name="...">...</property>
</bean>
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. In both cases, the Spring bean name has to include the module prefix.
If you want to avoid having to specify DelegatingActionProxy as Action type in your struts-config, for example to be able to generate your Struts config file with XDoclet, consider DelegatingRequestProcessor . The latter's disadvantage is that it might conflict with the need for a different RequestProcessor subclass.
The default implementation delegates to the DelegatingActionUtils class as fas as possible, to reuse as much code as possible with DelegatingRequestProcessor and DelegatingTilesRequestProcessor.