Spring With Struts 2 Integration Example by R4R Team

Spring With Struts 2 Integration

In this Topic We provide the integration between Struts 2 and Spring 3. Spring provides some features which are not available in struts 2. Most powerful among them is dependency injection.

Spring framework provides an easy way to manage the dependency. It can be easily integrated with struts 2 framework.

The ContextLoaderListener class is used to communicate spring application with struts 2. It must be specified in the web.xml file.

Here You need to follow following some steps to Integrate Spring with Struts 2 Application:

1 Create struts2 application and add spring jar files.

2 Create web.xml file, define ContextLoaderListener class.

3 Create 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. r4r.in.Login and id should match with the action class of struts.xml file (e.g. login).

5 In the action class, define extra property .
 


Login Example of Spring with Struts 2 Integration:-

In this example we provide the some steps to create the Application of Spring with Struts 2 as follows

Step 1: Project Structure

Create a Dynamic Web Project Struts2springIntegration in the Eclipse IDE.



Step 2: There are following jars file need to be added in WEB-INF/lib folder.

1 commons-fileupload-1.2.1

2 commons-io-1.3.2

3 commons-logging-1.1

4 freemarker-2.3.13

5 junit-3.8.1

6 ognl-2.6.11

7 struts2-convention-plugin-2.1.6

8 struts2-core-2.1.6

9 xwork-2.1.2

10 struts2-spring-plugin-2.1.6

11 antlr-runtime-3.0

12 org.springframework.asm-3.0.0.M3

13 org.springframework.beans-3.0.0.M3

14 org.springframework.context-3.0.0.M3

15 org.springframework.core-3.0.0.M3

16 org.springframework.expression-3.0.0.M3

17 org.springframework.web-3.0.0.M3

18 org.springframework.web.servlet-3.0.0.M3


Step 3:  Struts 2 + Spring 3 Plugin

To integrate Struts 2 and Spring, get and include the "struts2-spring-plugin-xxx.jar" library into your project classpath. As already added above struts2-spring-plugin-2.3.15.jar.


Step 4: Create web.xml file in WEB-INF folder.

This file 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"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Struts2SpringIntegration</display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<filter>

<filter-name>struts2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter-class>

</filter>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

 

Step 5: Create Login.java Class in package r4r.in.

This file defines four properties name, password, successlogin and errorlogin, with execute method where success is returned if password is admin.

package r4r.in;

public class Login {

private String name,password,successlogin,errorlogin;

public String execute(){

if(password.equals("admin"))

{

return "success";

}

else{

return "error";

}

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getSuccesslogin() {

return successlogin;

}

public void setSuccesslogin(String successlogin) {

this.successlogin = successlogin;

}

public String getErrorlogin() {

return errorlogin;

}

public void setErrorlogin(String errorlogin) {

this.errorlogin = errorlogin;

}

}

 

Step 6: Createstruts.xml file in WEB-INF folder.

This file 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"

"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<package name="struts" extends="struts-default">

<action name="login" class="login">

<result name="success">login.jsp</result>

<result name="error">error.jsp</result>

</action>

</package>

</struts>


Step 7: Create applicationContext.xml file in WEB-INF folder.

This file defines a bean with id login. This beans corresponds to the r4r.in.Login class. It will be considered as the action class here.

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

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="login" class="r4r.in.Login">

<property name="successlogin" value="You are successfully logged in!"></property>

<property name="errorlogin" value="Sorry, username or password Incorrect!"></property>

</bean>

</beans>

 

Step 8: Create index.jsp file in WebContent folder.

This file gets the name and password from the user.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Login Page</title>

</head>

<body>

<s:form action="login">

<s:textfield name="name" label="Username"></s:textfield>

<s:password name="password" label="Password"></s:password>

<s:submit value="login"></s:submit>

</s:form>

</body>

</html>

 

Step 9: Create login.jsp file in WebContent folder.

That prints values of userName and login properties.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Login Successs </title>

</head>

<body>

${successlogin} Welcome <s:property value="name"/>

</body>

</html>


Step 10: Create error.jsp file in WebContent folder.

This file define the error on a page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Error Page</title>

</head>

<body>

${errorlogin}

</body>

</html>

 

Step 11: Now Build and Compile the Project .

Step 12: Now run the project. The following Output shows:

 
Now the next Output image shows:

 

    

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!