Struts + Spring integration example
Here’s a tutorial to show how to access beans declared in the Spring Ioc container in a web application developed with Apache Struts 1.x.
Spring comes with “Struts-specific” solution for access beans declared in the Spring Ioc container.
- Register a Spring’s ready-make Struts plug-in in the Struts configuration file.
- Change your Struts action class to extend the Spring’s ActionSupport class, a subclass of the Struts Action class.
- The ActionSupport provide a convenient getWebApplicationContext() method for you to access beans declared in Spring Ioc container.
1. Struts + Spring dependencies
To integrate with Struts 1.x, Spring is required the “spring-web.jar” and “spring-struts.jar” libraries. You can download it from Spring web site or Maven.
pom.xml
<!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-struts</artifactId> <version>2.0.8</version> </dependency>
2. Register Struts plug-in
In your Struts configuration file (struts-config.xml), register the Spring’s ready-make Struts plug-in – “ ContextLoaderPlugIn“.
struts-config.xml
<struts-config> <!-- Spring Struts plugin --> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/classes/SpringBeans.xml" /> </plug-in> </struts-config>
The “ContextLoaderPlugIn” will handle all the integration work between Struts and Spring. You can load your Spring’s bean xml file into the “contextConfigLocation” property.
SpringBeans.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Beans Declaration --> <import resource="com/mkyong/customer/spring/CustomerBean.xml"/> </beans>
3. Spring’s ActionSupport
In Struts Action class, extends the Spring “ActionSupport” class, and get the Spring’s bean via “getWebApplicationContext()” method.
CustomerBean.xml
<bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" > <property name="customerDao" ref="customerDao" /> </bean>
Struts Action
package com.mkyong.customer.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.springframework.web.struts.ActionSupport; import com.mkyong.customer.bo.CustomerBo; import com.mkyong.customer.model.Customer; public class ListCustomerAction extends ActionSupport{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { CustomerBo customerBo = (CustomerBo) getWebApplicationContext().getBean("customerBo"); ... return mapping.findForward("success"); } }
Done.







[...] Spring Integration Dear mkyong, I have followed Step by step by this link Struts + Spring integration example. But i got some errors when starting server tomcat 7. Although i have added all jars. Apr 21, 2012 [...]
Urgent, Plz help me!!!
Unable to initialize Struts ActionServlet due to an unexpected exception or error thrown, so marking the servlet as unavailable. Most likely, this is due to an incorrect or missing library dependency.
java.lang.NoClassDefFoundError: org/apache/commons/chain/config/ConfigParser
at org.apache.struts.action.ActionServlet.initChain(ActionServlet.java:1680)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:350)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.Stan
: java.lang.ClassNotFoundException: org.apache.commons.chain.config.ConfigParser
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
… 14 more
Servlet /02StrutsSpringIntegration threw load() exception
javax.servlet.UnavailableException: org/apache/commons/chain/config/ConfigParser
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:402)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1190)
at org.apache.ca
http://www.mkyong.com/struts/struts-error-nosuchmethoderror-digester-parseljavaneturlljavalangobject/
Although, i tried to add all jar files. But still error.
My application in such way that integration of struts2-spring-Hibernate
so, send me all the Jar files needed to this application,plz
my mail ID is : (deleted to avoid spam)
This is a Maven project, get the
pom.xmlin download project and all the dependencies jars at you end.Hi,
This is not Struts + Spring Example, it looks struts+spring+hibernate, please post struts+spring example..
tq
[...] more details explanation, you can reference to the following tutorialsStruts + Spring integration exampleSpring + Quartz scheduler integration example [...]
Very useful tutorial.
Thank you.
[...] Struts + Spring integration example [...]
[...] Struts + Spring integration example [...]
[...] Struts + Spring integration Example to integrate Spring with Struts 1.x MVC framework. [...]
[...] Struts + Spring integration Example to integrate the Struts with Spring framework. [...]