<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this tutorial, it shows the integration between Struts 2 and Spring3. 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 <b><a href="https://dineshonjava.com/2012/06/dependency-injection-in-spring.html" target="_blank" rel="noopener">dependency injection in spring link</a></b>.</p>
<p>There are following tool we need to create this application.<br />
1. Strus2<br />
2. Spring3<br />
3. STS or Eclipse</p>
</div>
<h2><b>Step 1: Project Structure</b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/1111-2.png" border="0" /></div>
<h2><b>Step 2: There are following jars file need to be add</b><br />
<i></i></h2>
<p><i>commons-fileupload-1.2.1.jar</i><br />
<i>commons-io-1.3.2.jar</i><br />
<i>commons-lang-2.4.jar</i><br />
<i>commons-lang3-3.1.jar</i><br />
<i>commons-logging-1.0.4.jar</i><br />
<i>commons-logging-api-1.1.jar</i><br />
<i>freemarker-2.3.18.jar</i><br />
<i>javassist-3.0.jar</i><br />
<i>ognl-3.0.4.jar</i><br />
<i>spring-asm-3.0.1.RELEASE.jar</i><br />
<i>spring-beans-3.0.1.RELEASE.jar</i><br />
<i>spring-context-3.0.1.RELEASE.jar</i><br />
<i>spring-core-3.0.1.RELEASE.jar</i><br />
<i>spring-expression-3.0.1.RELEASE.jar</i><br />
<i>spring-jdbc-3.0.1.RELEASE.jar</i><br />
<i>spring-orm-3.0.1.RELEASE.jar</i><br />
<i>spring-tx-3.0.1.RELEASE.jar</i><br />
<i>spring-web-3.0.1.RELEASE.jar</i><br />
<i>spring-webmvc-3.0.1.RELEASE.jar</i><br />
<i>struts2-core-2.3.1.2.jar</i><br />
<i>struts2-dojo-plugin-2.3.15.jar</i><br />
<i>struts2-spring-plugin-2.3.15.jar</i><br />
<i>xwork-core-2.3.1.2.jar</i></p>
</div>
<div id="ads-id" align="center"></div>
<h2><b>Step 3: Struts 2 + Spring 3 Plugin</b></h2>
<p>To integrate Struts 2 and Spring, get and include the &#8220;<b>struts2-spring-plugin-xxx.jar</b>&#8221; library into your project classpath. As already added above <i>struts2-spring-plugin-2.3.15.jar</i>.</p>
<h2><b>Step 4: Spring Listener</b></h2>
<p>Configure the Spring listener &#8220;<i><b>org.springframework.web.context.ContextLoaderListener</b></i>&#8221; in web.xml file.</p>
<p><b>web.xml</b></p>
<pre class="highlight"><;?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_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Struts2Spring3Intergration<;/display-name>; 
 <;welcome-file-list>; 
 <;welcome-file>;User.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
 <;listener>; 
 <;listener-class>; 
 org.springframework.web.context.ContextLoaderListener 
 <;/listener-class>; 
 <;/listener>; 
 <;filter>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;filter-class>; 
 org.apache.struts2.dispatcher.FilterDispatcher 
 <;/filter-class>; 
 <;/filter>; 
 <;filter-mapping>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 
<;/web-app>; 
</pre>
<p>The important thing to note here is the listener that we have configured. The <i><b>ContextLoaderListener </b></i>is required to load the spring context file. Spring&#8217;s configuration file is called <i><b>applicationContext.xml</b></i> file and it must be placed at the same level as the <i><b>web.xml </b></i>file.</p>
<h2><b>Step 5: Register Spring Bean</b></h2>
<p>Register all the Spring’s Beans in the <b><i>applicationContext.xml</i></b> file, the Spring listener will locate this xml file automatically.<br />
<i><b>applicationContext.xml</b></i></p>
<pre class="highlight"><;?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:context="http://www.springframework.org/schema/context" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">; 
 
 <;context:component-scan base-package="com.dineshonjava.struts2.bean" />; 
 <;bean id="user" class="com.dineshonjava.struts2.action.user.UserAction">; 
 <;/bean>; 
 <;bean id="userBean" class="com.dineshonjava.struts2.bean.UserBean">; 
 <;/bean>; 
<;/beans>; 
</pre>
<h2><b>Step 7: Create Action class and User bean.</b><br />
<b></b></h2>
<p><b>UserAction.java</b></p>
<pre class="highlight">package com.dineshonjava.struts2.action.user; 
 
import org.springframework.beans.factory.annotation.Autowired; 
 
import com.dineshonjava.struts2.bean.UserBean; 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class UserAction extends ActionSupport implements ModelDriven<;UserBean>;{ 
 
 private static final long serialVersionUID = 1L; 
 
 @Autowired 
 private UserBean userBean; 
 
 public String execute() 
 { 
 return SUCCESS; 
 } 
 
 public String addUser() 
 { 
 return SUCCESS; 
 } 
 
 @Override 
 public UserBean getModel() { 
 return userBean; 
 } 
} 
</pre>
<p>Here <b>userBean </b>property <b>autowired </b>with action class <b>UserAction </b>java it is also powerfull feature of Spring framework.</p>
<p><i><b>UserBean.java</b></i></p>
<pre class="highlight">package com.dineshonjava.struts2.bean; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class UserBean { 
 private String userName; 
 private int userAge; 
 private String userGender; 
 private String userJob; 
 private String []userHobbies; 
 public String getUserName() { 
 return userName; 
 } 
 public void setUserName(String userName) { 
 this.userName = userName; 
 } 
 public int getUserAge() { 
 return userAge; 
 } 
 public void setUserAge(int userAge) { 
 this.userAge = userAge; 
 } 
 public String getUserGender() { 
 return userGender; 
 } 
 public void setUserGender(String userGender) { 
 this.userGender = userGender; 
 } 
 public String getUserJob() { 
 return userJob; 
 } 
 public void setUserJob(String userJob) { 
 this.userJob = userJob; 
 } 
 public String[] getUserHobbies() { 
 return userHobbies; 
 } 
 public void setUserHobbies(String[] userHobbies) { 
 this.userHobbies = userHobbies; 
 } 
} 
</pre>
<h2><b>Step 8: Struts.xml</b></h2>
<p>Declared all the relationship here.</p>
<pre class="highlight"><;?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>; 
 <;constant name="struts.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="true" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="user" extends="struts-default">; 
 <;action name="user" class="user" method="execute">; 
 <;result name="success">;/success.jsp<;/result>; 
 <;result name="input">;/User.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
 <;/struts>; 
</pre>
<p>The important thing to note is that we are using the id user to refer to the class. This means that we are using spring to do the dependency injection for the User class.<br />
<b></b></p>
<h2><b>Step 9: Create View</b></h2>
<p>Next let us create the <i><b>User.jsp</b></i> in the WebRoot folder:</p>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">; 
 
<;html>; 
<;head>; 
<;style type="text/css">; 
b{color: blue;} 
<;/style>; 
<;title>;Struts2 - Spring integration | dineshonjava.com<;/title>; 
<;/head>; 
<;body>; 
 
 <;h1>;<;span style="background-color: #FFFFcc">;User From Struts2 - Spring integration<;/span>;<;/h1>; 
 <;h2>;Add User<;/h2>;<;b>; 
 <;s:form action="user" method="addUser">; 
 <;s:textfield name="userName" key="user.name" />; 
 <;s:textfield name="userAge" key="user.age" value=""/>; 
 <;s:radio name="userGender" key="user.gender" list="{'Male','Female'}" />; 
 <;s:select name="userJob" key="user.job" list="%{#{'Software':'Software','Hardware':'Hardware','Networking':'Networking','Marketing':'Marketing'}}"/>; 
 <;s:checkboxlist name="userHobbies" key="user.hobby" list="{'Cricket','Football','Drawing','Cooking','Driving','Movie'}" />; 
 <;s:submit key="submit" align="center"/>; 
 <;/s:form>; 
 <;/b>; 
<;/body>; 
<;/html>; 
</pre>
<p><b>Creating <i>success.jsp</i> file for after submit.</b></p>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">; 
 
<;html>; 
<;head>; 
<;title>;Struts2 - Spring integration | dineshonjava.com<;/title>; 
<;/head>; 
<;body>; 
 
 <;h1>;<;span style="background-color: #FFFFcc">;User From Struts2 - Spring integration<;/span>;<;/h1>; 
 <;h2>;List of User<;/h2>; 
 <;ul>; 
 <;li>;User Name : <;s:property value="userName" />;<;/li>; 
 <;li>;User Age : <;s:property value="userAge" />;<;/li>; 
 <;li>;User Gender : <;s:property value="userGender" />;<;/li>; 
 <;li>;User Jobs : <;s:property value="userJob" />;<;/li>; 
 <;li>;User Hobbies : <;s:property value="userHobbies" />;<;/li>; 
 <;/ul>; 
<;/body>; 
<;/html>; 
</pre>
<p><i><b>myapp.properties</b></i></p>
<pre class="highlight">user.name=User Name 
user.age=User Age 
user.gender=Gender 
user.job=Job Type 
user.hobby=Hobbies 
submit=Add User 
</pre>
<p>Now right click on the project name and click <b>Export >; WAR</b> File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access</p>
<p><b>URL <i>http://localhost:8080/doj/User.jsp. </i></b></p>
<p>This will give you following screen:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/222.png" border="0" /></div>
</div>
<p><b>Clicking on &#8220;Add User&#8221; button then we will see following screen:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/333.png" border="0" /></div>
<p> ;</p>
</div>
<h2><b>Download Source Code + Libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2Spring3Intergration.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Struts2Spring3Intergration.zip</a></b></h2>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/struts-2-ajax-tags-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/struts2-integration-with-tiles2-spring3/">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/struts-2-ajax-tags-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/struts2-integration-with-tiles2-spring3/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '355'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…