Struts2 Spring 3 Integration Example

This is 8 of 8 part of struts 2 tutorial.

Tutorial Content:

    Introduction to struts 2 configuring struts 2 in eclipse Struts 2 hello world example Login page with validation in struts 2 Struts 2 interceptors with example File upload in struts 2 Struts 2 ajax example Struts 2 spring 3 integration example
In this post,you will see how to integrate Spring 3 and Struts 2. Now first question comes here ,why you want to integrate spring with struts 2? Spring provides some features which are not available in struts 2.Most powerful among them is dependency injection.To learn more about dependency injection,you can refer dependency injection in spring link.

Create project named "Struts2Spring3IntegrationExample".
First of all, you need to copy following files to WEB-INF/lib and add them to project's build path. You can download and install latest version of Spring Framework from
  • org.springframework.asm-3.1.1.RELEASE
  • org.springframework.beans-3.1.1.RELEASE
  • org.springframework.context-3.1.1.RELEASE
  • org.springframework.core-3.1.1.RELEASE
  • org.springframework.expression-3.1.1.RELEASE
  • org.springframework.web-3.1.1.RELEASE
  • org.springframework.web.servlet-3.1.1.RELEASE
Finally copy struts2-spring-plugin- to WEB-INF/lib from your downloaded struts2 jars.

For configuring all above jars and struts 2 jars in your eclipse ide please refer configuring struts 2 link.

Project structure:


Copy following content in web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Here you can note that we have configured listener.
org.springframework.web.context.ContextLoaderListner  is required to load spring configuration file.By default the applicationContext.xml file will be used for doing the Spring bean configuration and it must be same level as web.xml.

Action class:

Create action class named under package org.arpit.javaPostsForLearning in src folder.  
copy following content to
package org.arpit.javaPostsForLearning;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport{
    String message;
    public String getMessage() {
  return message;

    public void setMessage(String message) {
  this.message = message;
    public String execute()
   return SUCCESS;
Now we will configure above action file in applicationContext.xml.


Create applicationContext.xml in WebContent->WEB_INF. Copy following content to applicationContext.xml.
<?xml version="1.0" encoding="UTF-8"?>
    <bean id="helloWorldClass" class="org.arpit.javaPostsForLearning.HelloWorld" >
        <property name="message" value="Hello World! from arpit" />


We will create one jsp named "Welcome.jsp" under WebContent folder. Copy following code into Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    <%@taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 Tutorial</title>
<s:property value="message" />


Create struts.xml in src folder. Copy following content in struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    <package name="default" extends="struts-default" namespace="/">
        <action name="HelloWorld" class="helloWorldClass">
            <result name="success">Welcome.jsp</result>
Now you can note here that In <action> tag,we have set class attribute same as id of bean configured in applicationContext.xml.

Run Application:

Right click on project->run as->run on server.
copy http://localhost:8080/Struts2Spring3IntegrationExample/HelloWorld link to  browser and press enter.

Source Code :

click to begin
7.3MB .zip

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012