Struts 2 + Spring integration example
In this tutorial, it shows the integration between Struts 2 and Spring.
1. Project Structure
Here’s the project folder structure of this tutorials.

2. Struts 2 + Spring Plugin
To integrate Struts 2 and Spring, get and include the “struts2-spring-plugin-xxx.jar” library into your project classpath.
pom.xml
<!-- Struts 2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8</version> </dependency> <!-- 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> <!-- Struts 2 + Spring plugins --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.1.8</version> </dependency>
3. Spring Listener
Configure the Spring listener “org.springframework.web.context.ContextLoaderListener” in web.xml file.
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
3. Register Spring Bean
Register all the Spring’s Beans in the applicationContext.xml file, the Spring listener will locate this xml file automatically.
applicationContext.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"> <bean id="userBo" class="com.mkyong.user.bo.impl.UserBoImpl" /> <bean id="userSpringAction" class="com.mkyong.user.action.UserSpringAction"> <property name="userBo" ref="userBo" /> </bean> </beans>
UserBo.java
package com.mkyong.user.bo; public interface UserBo{ public void printUser(); }
UserBoImpl.java
package com.mkyong.user.bo.impl; import com.mkyong.user.bo.UserBo; public class UserBoImpl implements UserBo{ public void printUser(){ System.out.println("printUser() is executed..."); } }
UserSpringAction.java
package com.mkyong.user.action; import com.mkyong.user.bo.UserBo; public class UserSpringAction{ //DI via Spring UserBo userBo; public UserBo getUserBo() { return userBo; } public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String execute() throws Exception { userBo.printUser(); return "success"; } }
5. Struts.xml
Declared all the relationship here.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="userAction" class="com.mkyong.user.action.UserAction" > <result name="success">pages/user.jsp</result> </action> <action name="userSpringAction" class="userSpringAction" > <result name="success">pages/user.jsp</result> </action> </package> </struts>
6. Demo
Now, all the Struts 2 and Spring integration work is done, now see the following use case to access the Spring’s “userBo” bean.
- Case 1 : Make Spring act as the Struts 2 Action class, and access the Spring’s bean.
- Case 2 : Access the Spring’s bean in Struts 2 Action class.
Case 1
In this example, the userSpringAction is act as the Struts 2 Action class, and you can DI the Spring’s userBo bean with normal Spring’s way.
//struts.xml <action name="userSpringAction" class="userSpringAction" > <result name="success">pages/user.jsp</result> </action> //applicationContext.xml <bean id="userSpringAction" class="com.mkyong.user.action.UserSpringAction"> <property name="userBo" ref="userBo" /> </bean>
To access this action, use the URL : http://localhost:8080/Struts2Example/userSpringAction
Case 2
By default, Spring listener enables “autowiring by matching the bean name“. So, it will pass the Spring’s “userBo” bean into the UserAction via setUserBo() automatically. See below Struts 2 Action :
UserAction.java
package com.mkyong.user.action; import com.mkyong.user.bo.UserBo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ //DI via Spring UserBo userBo; public UserBo getUserBo() { return userBo; } public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String execute() throws Exception { userBo.printUser(); return SUCCESS; } }
To access this action, use the URL : http://localhost:8080/Struts2Example/userAction
Alternatively, you can use the Spring’s generic WebApplicationContextUtils class to get the Spring’s bean directly.
package com.mkyong.user.action; import org.apache.struts2.ServletActionContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.mkyong.user.bo.UserBo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ public String execute() throws Exception { WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext( ServletActionContext.getServletContext() ); UserBo userBo1 = (UserBo)context.getBean("userBo"); userBo1.printUser(); return SUCCESS; } }
A really long and tedious article, make sure you download the full project source code for practice.
Hi mkyong,
I’m getting below error while starting the server.Please advise.
Caused by: java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.classloader.ReloadingClassLoader
Thanks.I was using a Xwork2 older version.Changed to latest version.
Hi mkyong, thank you for your tutorial. I’ve download 3 your example project. But when i import the project to my eclipse.,all of it get error like this in pom.xml
- Failure to transfer org.codehaus.plexus:plexus-archiver:jar:1.2 from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.codehaus.plexus:plexus-archiver:jar:1.2 from/to central (http://repo1.maven.org/maven2): No route to host: no further information to http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/1.2/plexus-archiver-1.2.jar
- Missing artifact com.sun:tools:jar:1.5.0
- Missing artifact javax.transaction:jta:jar:1.0.1B
Why i got this error.,could you tell me how to resolve this?
Thank you..,
Hi mkyong ,
thanx alot for your fantastic work.I have a question can i inject a bean in the bao without inject bao in Action ?
[...] web framework.Struts + Spring integration Example to integrate Spring with Struts 1.x framework.Struts 2 + Spring integration example Example to integrate Spring with Struts 2 framework.JSF 2.0 + Spring integration example Example to [...]
Hi mkyong,
I always like your blogs on Struts and Spring, really useful. i need your small help, do you have Struts2.2.1 and Spring3.0.5 integrated application ? if you have then please send it to my mail id.
Thanks..
A.R.U
Hi Arun, i don’t have the Struts2 + Spring3 integration at the moment, will study on it and let you know the result.
Hi mkyong,
Thanks a lot for your reply. I hope i will get the result soon. i am also tring for that.
Thanks..
A.R.U
Just wanted to let you know if you use Spring 3 along with Struts 2, you will face a problem (if using Maven). The reason being that the the Struts jars have dependency on old spring versions. That causes a problem.
The solution is to use maven dependency management to force all spring dependencies to resolve to the latest version. (3.0.5 in my case).
[...] Struts 2 + Spring integration example [...]
[...] In this tutorial, it shows how to integrate Struts 2 + Spring + Quartz scheduler together. The relationship as following : Struts 2 <–Struts2 Spring Plugin –> Spring <–(Spring-Helper)–> Quartz <—> Scheduler task This article will do a little explanation on the Struts 2 and Spring integration, for details, please access this Struts 2 + Spring integration example. [...]