Struts2 Spring 3 Integration Example

Struts2 Spring 3 Integration Example

Now first question comes here ,why you want to integrate spring with struts 2? Spring provides some features which are not available in struts 2.Most powerful among them is dependency injection.To learn more about dependency injection,you can refer dependency injection in spring link.
Create project named “Struts2Spring3IntegrationExample”.
First of all, you need to copy following files to WEB-INF/lib and add them to project’s build path. You can download and install latest version of Spring Framework from http://www.springsource.org/download

  • org.springframework.asm-3.1.1.RELEASE
  • org.springframework.beans-3.1.1.RELEASE
  • org.springframework.context-3.1.1.RELEASE
  • org.springframework.core-3.1.1.RELEASE
  • org.springframework.expression-3.1.1.RELEASE
  • org.springframework.web-3.1.1.RELEASE
  • org.springframework.web.servlet-3.1.1.RELEASE

Finally copy struts2-spring-plugin-2.3.1.2 to WEB-INF/lib from your downloaded struts2 jars.

For configuring all above jars and struts 2 jars in your eclipse ide please refer configuring struts 2 link.

 

There are following jars file need to be add
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
freemarker-2.3.18.jar
javassist-3.0.jar
ognl-3.0.4.jar
spring-asm-3.0.1.RELEASE.jar
spring-beans-3.0.1.RELEASE.jar
spring-context-3.0.1.RELEASE.jar
spring-core-3.0.1.RELEASE.jar
spring-expression-3.0.1.RELEASE.jar
spring-jdbc-3.0.1.RELEASE.jar
spring-orm-3.0.1.RELEASE.jar
spring-tx-3.0.1.RELEASE.jar
spring-web-3.0.1.RELEASE.jar
spring-webmvc-3.0.1.RELEASE.jar
struts2-core-2.3.1.2.jar
struts2-dojo-plugin-2.3.15.jar
struts2-spring-plugin-2.3.15.jar
xwork-core-2.3.1.2.jar

 

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.

Project structure:

 struts

Web.xml:

Copy following content in web.xml.

<?xml version=”1.0″ encoding=”UTF-8″?>  <web-app id=”WebApp_ID” version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>

<display-name>Struts2Spring3IntegrationExample</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>default.jsp</welcome-file>

</welcome-file-list>

</web-app>
Here you can note that we have configured listener.
org.springframework.web.context.ContextLoaderListner  is required to load spring configuration file.By default the applicationContext.xml file will be used for doing the Spring bean configuration and it must be same level as web.xml.
Action class:

Create action class named HelloWorld.java under package org.arpit.javaPostsForLearning in src folder.

copy following content to HelloWorld.java.

package org.arpit.javaPostsForLearning;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{

String message;

public String getMessage() {

return message;   }

public void setMessage(String message) {

this.message = message;

}

public String execute()

{

return SUCCESS;

}

}

Now we will configure above action file in applicationContext.xml.
applicationContext.xml

Create applicationContext.xml in WebContent->WEB_INF. Copy following content to 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=”org.arpit.javaPostsForLearning.HelloWorld” >

<property name=”message” value=”Hello World! from arpit” />

</bean>

</beans>

JSPs:

We will create one jsp named “Welcome.jsp” under WebContent folder. Copy following code into Welcome.jsp

 

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

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

<html>

<head>

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

<title>Struts 2 Tutorial</title>

</head>

<body>

<s:property value=”message” />

</body>

</html>

struts.xml:

Create struts.xml in src folder. Copy following content in struts.xml

view plainprint?

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!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” namespace=”/”>          <action name=”HelloWorld” class=”helloWorldClass”>

<result name=”success”>Welcome.jsp</result>

</action>

</package>

</struts>

Now you can note here that In <action> tag,we have set class attribute same as id of bean configured in applicationContext.xml.

Run Application:

Right click on project->run as->run on server.
copy http://localhost:8080/Struts2Spring3IntegrationExample/HelloWorld link to  browser and press enter.

struts2
We have now seen how to bring two great frameworks together.

 

Enter Your Comment

Your email address will not be published. Required fields are marked *