Struts 2 Spring Integration

Struts 2 Spring Integration

Posted in : Java Posted on : November 24, 2011 at 6:39 PM Comments : [ 0 ]

In this section, you will learn how to integrate Struts2 with Spring framework.

Struts 2 Spring Integration

In this section, you will learn how to integrate Struts2 with Spring framework.


In this example, you will learn integration of  Struts2 with Spring framework using the struts2-spring-plugin. Due to this, we are able to use Dependency Injection in Struts.

The project hierarchy is given below :

The jar file used in the application is given below :

The configuration of the web.xml is given below :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
xmlns="" xmlns:web=""
id="WebApp_ID" version="2.5">

By default, for Spring bean configuration applicationContext.xml is used :

<?xml version="1.0" encoding="UTF-8"?>
<bean id="messageHelloWorld" class="com.devmanuals.Message" >
<property name="message" value="Hello World!" />

In the above code, we inject the "Hello World" message to message attribute using setter injection

The action configuration in struts.xml is given below :

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<package name="default" extends="struts-default">
<action name="message" class="messageHelloWorld">
<result name="SUCCESS">/message.jsp</result>

In the above code, see carefully instead of giving com.devmanuals.Message class qualified name, we are providing messageHelloWorld which is the bean name in the Spring configuration file applicationContext.xml .

The code of action class is given below :

package com.devmanuals;

public class Message {

private String message;

public String getMessage() {
return message;

public void setMessage(String message) {
this.message = message;

public String execute() {
return "SUCCESS";

The forward.jsp code is given below :

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=message.action">

The message.jsp code is given below :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>


When you execute application, you will get the following output:

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics