Struts2 +Spring2.5 Integration
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
1.Create table in database(system/manager account)
1
2
3
4
5
| SQL> create table userdetails(username varchar2(10),password varchar2(10)); |
2.Develop all the resources manually
register.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> |
success.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.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
25
| <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.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.jdbc.driver.OracleDriver"/></bean><!--configuring DBStore,having persistence logic--><bean id="dbs"><property name="ds" ref="drds"/></bean><!-- configuring Action class--><bean id="regAction"><property name="dbStore" ref="dbs"/></bean></beans> |
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| <web-app><filter><filter-name>struts2</filter-name><filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>register.jsp</welcome-file></welcome-file-list><listener><listener-class> org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app> |
struts.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.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<struts><!-- This tells the struts,that Injection(for Action Class) will be done by spring--><constant name="struts.objectFactory" value="spring" /><!--<constant name="struts.devMode" value="true" />--><package name="myPack" extends="struts-default"><!--doesnot look for class,but looks for bean with name "regAction" in Spring cfg file--><action name="reg" class="regAction"><result name="SUCCESS">success.jsp</result></action></package></struts> |
DBStore.java
1
2
3
4
5
6
7
| package p1;public interface DBStore{public void storeUser(String uname,String password);} |
DBStoreImpl.java
1
2
3
4
5
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
| package p1;import javax.sql.*;import java.sql.*;public class DBStoreImpl implements DBStore{Connection con;DataSource ds;public void setDs(DataSource ds){System.out.println("setDs() of selectBean"+ds);this.ds=ds;}public void storeUser(String uname,String password){try{con=ds.getConnection();PreparedStatement ps=con.prepareStatement("insert into userdetails values(?,?)");ps.setString(1, uname);ps.setString(2,password);int count=ps.executeUpdate();if(count!=0)System.out.println("Successful");else System.out.println("Not Successful");con.close();}catch(Exception e){e.printStackTrace();//con.close();}}} |
RegisterAction.java (struts Action Class)
1
2
3
4
5
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
| package p1;import javax.sql.*;import java.sql.*;public class DBStoreImpl implements DBStore{Connection con;DataSource ds;public void setDs(DataSource ds){System.out.println("setDs() of selectBean"+ds);this.ds=ds;}public void storeUser(String uname,String password){try{con=ds.getConnection();PreparedStatement ps=con.prepareStatement("insert into userdetails values(?,?)");ps.setString(1, uname);ps.setString(2,password);int count=ps.executeUpdate();if(count!=0)System.out.println("Successful");else System.out.println("Not Successful");con.close();}catch(Exception e){e.printStackTrace();//con.close();}}} |
Explaination
1.User deploys the application.When Servlet Container loads our application,it reads web.xml file and it activates the Spring framework by looking at following code.
1
2
3
4
5
6
7
| <!-- see web.xml for more details--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> |
By default Spring will look for a configuration file name applicationContext.xml in WEB-INF folder.
2.It then immediately reads applicationContext.xml,therefore creating bean objects.
It configures the datasource object(drds),pointing to oracle database
It creates DBStoreImpl bean object (dbs),and injects drds into it
It finally creates RegisterAction bean object(regAction) and injects dbs to it
It configures the datasource object(drds),pointing to oracle database
It creates DBStoreImpl bean object (dbs),and injects drds into it
It finally creates RegisterAction bean object(regAction) and injects dbs to it
3.User give request to
http://localhost:2012/Struts2Spring/
4.Due to <welcome-file>configured in web.xml,register.jsp is given back to user.He enters username,password as satya/prasad and clicks submit button
5.As Filter is configured in web.xml,StrutsPrepareAndExecuteFilter traps and takes the request.In the request we have “action=reg”(see register.jsp),As a result Struts will look into struts.xml file to decide the Action class.The moment above filter reads the struts.xml file
1
| <constant name="struts.objectFactory" value="spring" /> |
,it understands that Action class will be injected through Spring Dependency Injection.
Now,As per our “action=reg” (register.jsp),it looks at the following code (struts.xml)
1
2
3
| <action name="reg" class="regAction"><result name="SUCCESS">success.jsp</result></action> |
Note: regAction is not the Action class name,but it is the bean name that is configured in applicationContext.xml.So Here Spring Dependency Injection will take place.Action class Object will be created and injected by Spring.So the class name of<action ….> should match with <bean id=” “> value
1
2
3
4
| <!-- configuring Action class--><bean id="regAction" class="p1.RegisterAction"><property name="dbStore" ref="dbs"/></bean> |
6.Now,After Action class gets injected,its execute() will be executed,where we keep our business logic.In our RegisterAction class,we have injected DBStoreImpl Object(as dbStore)
dbStore.storeUser(username,password); will store the username,password into database
7.execute() returns SUCCESS as a string.StrutsPrepareAndExecuteFilter looks for SUCCESS message in the struts configuration file,to decide the result page.It finds that success.jsp is responsible for displaying the result page.Hence the formatted output of success.jsp will be send to the browser.

No comments:
Post a Comment