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.

EXAMPLE

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="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2SpringIntigration</display-name>
<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>
<welcome-file-list>
<welcome-file>forward.jsp</welcome-file>
</welcome-file-list>
</web-app>

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="messageHelloWorld" class="com.devmanuals.Message" >
<property name="message" value="Hello World!" />
</bean>
</beans>

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 :

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

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

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 Message.java 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"
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>Hello World</title>
</head>
<body>
${message} 
</body>
</html>

OUTPUT

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