Pages Navigation Menu

Coding is much easier than you think

Struts 2 + Spring Integration Example

 
In this following example we shall see how to integrate Spring and Struts 2 using the struts2-spring-plugin. We integrate Spring with Struts2 to utilize the Spring’s Dependency Injection feature.

First add the org.springframework.web.context.ContextLoaderListener to the web.xml file.
 
** UPDATE: Spring Complete tutorial now available here.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
File : web.xml  
 

<?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>Struts2</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>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

By default the applicationContext.xml file will be used for doing the Spring bean configuration.
 
File : applicationContext.xml  
 

<?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="helloWorldClass" class="com.simplecode.HelloWorld" >
    	<property name="msg" value="Struts 2 + Spring Integration Example!" />
    </bean>
</beans>

 
As you can see we have registered the HelloWorld class and injected the “Hello World!” message to the message attribute using the setter injection method.

All the Struts 2 action configuration goes in the struts.xml file.
 
File : struts.xml  
 

<!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="helloWorld" class="helloWorldClass">
        <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>

 

The only change here is instead of referring the com.simplecode.HelloWorld class directly, we relate it using the bean name given in the spring bean configuration file.

The HelloWorld class is shown below. In the execute() method we simply return “SUCCESS” and themessage attribute is set using setter injection.
 
File : HelloWorld.java  
 

package com.simplecode;

public class HelloWorld
{
	private String msg;

	public String getMsg()
        {
		return msg;
	}

	public void setMsg(String msg)
        {
		this.msg = msg;
	}
	
	public String execute()
        {
        return "SUCCESS";
        }
}

 

In the index.jsp page we forward the request to the helloWorld action.

 

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=helloWorld.action">

 
After invoking the execute() method the user will be directed to the success.jsp page. In this page we dispaly the message value.

 

struts2Spring

 

You need to have the following jar files in the WEB-INF/lib directory.

 

commons-fileupload-1.2.1
commons-io-1.3.2
commons-logging-1.1
freemarker-2.3.13
junit-3.8.1
ognl-2.6.11
struts2-convention-plugin-2.1.6
struts2-core-2.1.6
xwork-2.1.2
struts2-spring-plugin-2.1.6
antlr-runtime-3.0
org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3
org.springframework.web-3.0.0.M3
org.springframework.web.servlet-3.0.0.M3

 

About Mohaideen Jamil


    %d bloggers like this: