Spring Tutorials

Spring Tutorial

Introduction of Spring Framework

Advantages and Disadvantages of Spring Framework

Features of Spring Framework

Basic concept of Spring Framework

Architecture of Spring Framework

Modules of Spring Framework

Goal of Spring Framework

Create Application of Spring without any IDE

Create Application of Spring in Eclipse IDE

Create Application of Spring in MYEclipse IDE

Important JAR of Spring Framework

IOC container in Spring Framework

Bean and Applicationcontext container in Spring Framework

Example of Spring BeanFactory Container in Spring Framework

Example of Spring ApplicationContext Container in Spring Framework

Bean in Spring Framework

Lifecycle of Bean in Spring Framework

Scope of Bean in Spring Framework

Autowiring in Spring Framework

Dependency Injection in Spring Framework

Constructor-based Dependency Injection Example in Spring Framework

Setter-based Dependency Injection Example in Spring Framework

Introduction of AOP in Spring Framework

Core concept and Goal of AOP in Spring Framework

AOP Proxies in Spring framework

XML Schema Based AOP in Spring Framework

AOP Xml based Configuration Example in Spring Framework

Example of Declaring AOP Advices in Spring Framework

AOP AspectJ Annotation with Example in Spring Framework

Declaring an aspect and pointcut using Annotation in Spring Framework

Declaring AOP Advices using Annotation Example in Spring Framework

DAO support in Spring Framework

Introduction of Spring JDBC Framework in Spring Framework

Introduction of Spring JdbcTemplate in Spring Framework

Example of Spring JdbcTemplate class in Spring framework

Example of Executing SQL statements in Spring Framework

Example of Executing DDL statements in Spring Framework

SQL Stored Procedure in Spring Framework

Example of NamedParameterJdbcTemplate in Spring Framework

Example of RowMapperJdbcTemplate in Spring Framework

Introduction of ORM Framework in Spring Framework

Integration of Spring with Hibernate in Spring Framework

Integration of Spring with JPA in Spring Framework

Introduction of Spring Expression Language (SpEL) in Spring Framework

Example of Spring Expression Language (SpEL) in Spring framework

Example of Spring EL in XML and Annotation in Spring Framework

Language Reference with SpEL in Spring Framework

Operators in Spring Expression Language(SpEL) in Spring Framework

Variable in Spring Expression Language(SpEL) in Spring Framework

Introduction of Spring Framework Transaction Management in Spring Framework

Spring Framework Transaction Abstraction

Spring Declarative Transaction Management in Spring Framework

Spring Programmatic Transaction Management in Spring Framework

Introduction of Spring OXM (Object XML Mapping) in Spring Framework

Integration of Spring with Jaxb Example in Spring framework

Example of Spring with Xstream in Spring Framework

Example of Spring with Castor in Spring Framework

Remote Method Invocation(RMI) in Spring Framework

Spring and RMI Integration with Example in Spring Framework

Example of Spring and Httpinvoker in Spring Framework

Example of Spring and Hessian in Spring Framework

Integration of Spring with JMS in Spring Framework

Introduction of Webservice in Spring Framework

Spring Web Services in Spring Framework

Web Services with Jax-WS in Spring framework

Exposing and Exporting servlet-based Web Services using JAX-WS in Spring Framework

Accessing Web Services using JAX-WS in Spring Framework

Introduction of JMS in Spring Framework

JMS Messaging Models in Spring Framework

Using Spring JMS in Spring Framework

Sending and Receiving a Message Using JMS API in Spring Framework

Introduction of JMX (Java Management Extension) in Spring Framework

Integrating Beans with JMX in Spring Framework

Creating a MBeanServer in Spring Framework

Introduction Java Mail with Spring in Spring Framework

Example of Java Mail with spring in Spring Framework

Introduction of EJB(Enterprise JavaBeans) in Spring Framework

Introduction of EJB(Enterprise JavaBeans) Integration in Spring Framework

Integration of Spring With Struts 2 Example in Spring Framework

Spring MVC

Spring MVC

Spring With Struts 2 Integration Example
Previous Home Next

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. com.r4r.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 SpringStrutsIntegration 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>SpringStrutsIntegration</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 com.r4r.

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

package com.r4r;
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: Create struts.xml file in Src 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 com.r4r.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="com.r4r.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}
<jsp:include page="index.jsp"></jsp:include>
</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:

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: