Struts 2 + Spring Integration Example

Struts 2 provides plug-ins for integration to any other frameworks. If you are using Spring for your bean management, then it is very easy for you to integrate Spring container to Struts 2 application. Spring can manage the beans and struts actions classes as beans. You have to just specify the bean name in the struts configuration file. The following steps to be taken for the Struts 2 and Spring integration.

  1. Download the Struts2-Spring-plugin.jar. It is the library which integrates both the frameworks.
  2. Add Spring libraries into the lib folder
  3. Add context loader in the web.xml for loading the spring configuration file.

Struts 2 Spring JAR Files

The above steps are the additional tasks which you have to do with the Struts 2 application for enabling the integration. This tutorial shows a very simple example for the integration. Lets look at the example code.

1. Action Class and Bean

Write a action class and declare a bean which will be injected from the spring container with default values.

public class Struts2SpringIntegrationAction {
	//Spring Injection
	private UserDetails userDetails;

	public UserDetails getUserDetails() {
		return userDetails;

	public void setUserDetails(UserDetails userDetails) {
		this.userDetails = userDetails;

	public String execute(){
		return "success";


public class UserDetails {
	private String userName;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;


2. Spring Configuration File

It is the traditional spring configuration file for declaring the spring beans. Note that here I have declared Struts action class as the spring beans and injected UserDetails beans to the action class.


<beans xmlns=""
 	<bean id="userDetails" class="">
 		<property name="userName" value="Rahul Dravid"/>
	<bean id="struts2Spring" class="">
		<property name="userDetails" ref="userDetails"/>

3. Struts 2 Configuration File

Struts 2 configuration file would use the spring’s bean name in the class attribute of the action mapping.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "">
<constant name="struts.devMode" value="true" />
<package name="uitagsdemo" extends="struts-default">
	<action name="Struts2Spring" class="struts2Spring" >
		<result name="success">Result.jsp</result>

4. JSP File

Write a simple JSP to print the values.


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
	<h2>JavaBeat - Struts 2 Spring Integration Demo</h2>
		<s:push value="userDetails">
			User Name :	<s:property value="userName" /><br>

5. Web.xml

There are two entries in the web.xml

  1. Struts 2 filter and
  2. Spring configuration loader
<?xml version="1.0" encoding="UTF-8"?>

6. Struts 2 Spring Integration Demo

If you access the application using URL http://localhost:8080/Struts2App/Struts2Spring, you would see the following output.

Struts 2 Spring Integration Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!