How to integrate Struts2 with Spring 2.5 I assume you have installed JAVA,TOMCAT6.

0,ORACLE 10G(Express Edition) Program Flow:In this program,we have a form page,where user enters his username,password and submits the form.The form data will be stored in DB(userdetails table). The form page is developed using struts2 supplied JSP tags.Spring is used for dependency injection,to create & inject Action class Directory Structure

2. password varchar2(10) ).Create table in database(system/manager account) SQL> create table userdetails ( username varchar2(10).jsp ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <%@ taglib prefix="html" uri="/struts-tags" %> <html> <head></head> <body> <h1>User Registeration Page</h1> <html:form action="reg"> <html:textfield name="username" label="Username"/> <html:password name="password" label="Password"/> <html:submit value="Register"/> </html:form> </body> </html> .1.Develop all the resources manually register.

jsp ? 1 2 3 4 5 6 7 8 9 10 ? <%@ taglib prefix="myHtml" uri="/struts-tags" %> <html> <head></head> <body> <h1>Success Page</h1> <h4>Hello <myHtml:property value="username"/></h4> </body> </html> applicationContext.filter.xml (Spring Configuration File) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ? <beans xmlns="http://www.apache.org/schema/beans" xmlns:xsi="http://www.driver.xsd"> <!--Configuring datasource--> <bean id="drds"> <property name="username" value="system"/> <property name="password" value="manager"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/> <property name="driverClassName" value="oracle.w3.org/schema/beans/spring-beans-2.5.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dispatcher.jdbc.ng.OracleDriver"/> </bean> <!--configuring DBStore.success.springframework.configuring Action class--> <bean id="regAction"> <property name="dbStore" ref="dbs"/> </bean> </beans> web.org/schema/beans http://www.springframework.springframework.StrutsPrepareAndExecuteFilter </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </filter- .struts2.having persistence logic--> <bean id="dbs"> <property name="ds" ref="drds"/> </bean> <!-.xml 1 2 3 4 5 6 7 8 9 10 <web-app> <filter> <filter-name>struts2</filter-name> <filter-class> org.

import javax. import java.springframework.jsp</welcome-file> </welcome-file-list> <listener> <listener-class> org.devMode" value="true" />--> <package name="myPack" extends="struts-default"> <!--doesnot look for class.objectFactory" value="spring" /> <!--<constant name="struts.dtd"> <struts> <!-.apache.web.*.ContextLoaderListener</listener-cl </listener> </web-app> struts. } DBStoreImpl.but looks for bean with name "regAction" cfg file--> <action name="reg" class="regAction"> <result name="SUCCESS">success.0.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.that Injection(for Action Class) will be done by spring--> <constant name="struts.org/dtds/struts-2.java ? 1 2 3 4 5 package p1.xml (Struts configuration file) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ? <?xml version="1.context.jsp</result> </action> </package> </struts> in Spring DBStore.This tells the struts.0//EN" "http://struts. public interface DBStore{ public void storeUser(String uname.java 1 2 3 4 5 6 7 package p1. public class DBStoreImpl implements DBStore{ .*.sql.sql.11 12 13 14 15 16 17 18 19 20 21 ? <welcome-file-list> <welcome-file>register.String password).

getConnection(). DataSource ds.printStackTrace(). } } } RegisterAction.6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 ? Connection con.?)"). int count=ps. this. } catch(Exception e) { e. uname). public void setDbStore(DBStore dbs) { dbStore=dbs.out.java (struts Action Class) 1 2 3 4 5 6 7 8 9 10 11 12 package p1.close().String password) { try { con=ds. else System.executeUpdate().prepareStatement("insert into userdetails values(?.out.setString(1.println("setDs() of selectBean"+ds).println("Successful"). public void setDs(DataSource ds) { System. ps.println("Not Successful"). PreparedStatement ps=con. } public void storeUser(String uname. ps. //con. } private String username.password).setString(2. //getters and setters public String getUsername() { .out.close(). con.ds=ds. public class RegisterAction { private DBStore dbStore.password. if(count!=0) System.

xml file and it activates the Spring framework by looking at following code.It then immediately reads applicationContext.therefore creating bean objects.context.xml.13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 return username. dbStore.println(" Execute Method called"). } public void setUsername(String username) { this.xml for more details--> <listener> <listener-class> org. return "SUCCESS".xml in WEB-INF folder. 2.out.User deploys the application.storeUser(username.see web. It configures the datasource object(drds).ContextLoaderListener </listener-class> </listener> By default Spring will look for a configuration file name applicationContext.When Servlet Container loads our application.username = username. ? 1 2 3 4 5 6 7 <!-.User give request to http://localhost:2012/Struts2Spring/ . } // all struts logic here public String execute() { System. } } Explaination 1.and injects drds into it It finally creates RegisterAction bean object(regAction) and injects dbs to it 3. } public String getPassword() { return password.password).springframework.it reads web.password = password. } public void setPassword(String password) { this.web.pointing to oracle database It creates DBStoreImpl bean object (dbs).

password).register. will store the username.As a result Struts will look into struts.jsp is given back to user.objectFactory" value="spring" /> .where we keep our business logic.it looks at the following code (struts.to decide the result page.Now.As per our “action=reg” (register.xml file ? 1 <constant name="struts.xml.RegisterAction"> <property name="dbStore" ref="dbs"/> </bean> 6.Due to <welcome-file>configured in web.jsp).execute() returns SUCCESS as a string.it understands that Action class will be injected through Spring Dependency Injection.xml) ? 1 2 3 <action name="reg" class="regAction"> <result name="SUCCESS">success.its execute() will be executed. Now.StrutsPrepareAndExecuteFilter looks for SUCCESS message in the struts configuration file.So Here Spring Dependency Injection will take place.After Action class gets injected.we have injected DBStoreImpl Object(as dbStore) dbStore.xml.jsp).configuring Action class--> <bean id="regAction" class="p1.So the class name of<action ….xml.The moment above filter reads the struts.It finds that success.As Filter is configured in web.In our RegisterAction class.password as satya/prasad and clicks submit button 5.but it is the bean name that is configured in applicationContext.StrutsPrepareAndExecuteFilter traps and takes the request.> should match with <bean id=” “> value ? 1 2 3 4 <!-. .In the request we have “action=reg”(see register.jsp is responsible for displaying the result page.4.jsp will be send to the browser.storeUser(username.xml file to decide the Action class.jsp</result> </action> Note: regAction is not the Action class name.Action class Object will be created and injected by Spring.password into database 7.He enters username.Hence the formatted output of success.