HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

struts-spring: How to integrate struts with spring?

Please give me a sample project of struts2 integration with spring.

struts x 31
spring x 24
Posted On : 2015-07-10 23:42:10.0
profile Garima Gupta - Garima Gupta


The ContextLoaderListener class is used to communicate spring application with struts 2. It must be specified in the web.xml file. and applicationContext.xml should be in WEB-INF.

You need to follow the following steps:
1) Create struts2 application and add spring jar files.
2) In web.xml file, define ContextLoaderListener class.
3) In struts.xml file, define bean name for the action class.
4) In applicationContext.xml file, create the bean. Its class name should be action class name e.g. in.anyforum.Login and id should match with the action class of struts.xml file (e.g. login).
5) In the action class, define extra property e.g. message.

Example of Spring and Struts 2 Integration:
You need to create following files for simple spring and struts 2 application:

1. index.jsp
2. web.xml
3. struts.xml
4. applicationContext.xml
6. welcome.jsp
7. error.jsp

1) index.jsp:
This page gets the name from the user.

<%@ taglib uri="/struts-tags" prefix="s"%>

<s:form action="login">
<s:textfield name="userName" label="UserName"></s:textfield>

2) web.xml:
It defines controller class for struts 2 and ContextLoaderListener listener class to make connection between struts2 and spring application.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"




3) struts.xml:
It defines the package with action and result. Here, the action class name is login which will be searched in the applicationContext.xml file.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
<package name="abc" extends="struts-default">
<action name="login" class="login">
<result name="success">welcome.jsp</result>



4) applicationContext.xml:
It defines a bean with id login. This beans corresponds to the mypack.Login class. It will be considered as the action class here.

It should be located inside the WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>

<bean id="login" class="in.anyforum.Login">
<property name="message" value="Welcome Spring"></property>


It defines two property userName and message with execute method where success is returned.

package in.anyforum;
public class Login {
private String userName,message;

public String getMessage() {
return message;
public void setMessage(String message) {
this.message = message;
public String getUserName() {
return userName;
public void setUserName(String userName) {
this.userName = userName;
public String execute(){
return "success";

6) welcome.jsp:
It prints values of userName and message properties.

<%@ taglib uri="/struts-tags" prefix="s"%>

Welcome, <s:property value="userName"/><br/>

7) error.jsp:
It is the error page. But it is not required in this example because we are not defining any logic in the execute method of action class.

Sorry! Some thing went Wrong...

Click Here to Download Struts-Spring Integration Example Using MyEclipse IDE

Output Struts-Spring Example
Output Struts-Spring Example

Posted On : 2015-07-11 00:14:01
Satisfied : 1 Yes  0 No
profile Rishi Kumar - Rishi Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -