Struts, Hibernate and Spring Integration - A Case Study
Struts, Hibernate and Spring Integration - A Case Study
Abstract Over the last few decades software development has undergone tremendous radical changes in order to enhance user experience, user
friendliness and to widen its scope over large geographical area. The key actors in this arena are two leading IT companies Microsoft and Sun
Microsystems, now taken owner by Oracle Inc. who compete on a continual basis for rendering rapid professional software design and
development process at the same time incorporating more functionality and focusing strongly towards software maintenance issues. Due to this
intense competition, the ultimate beneficiary is a software developer who is tremendously benefited at large. In this review paper, the researcher
aims at consolidating the technological advancements that have brought a revolutionary change in corporate software development over last few
decades. The main substance of the paper is technological advancements facilitating J2EE application development viz., struts framework,
hibernate and spring framework which operate in different layers of scalable N-tier architecture. Each technology has its own merits and de-
merits. The researcher attempts to aggregate the benefits offered by the trio in a single J2EE application thereby bringing in best of three worlds
to a single application. The application is boosted with powerful struts tag library, persistent layer provided by hibernate and dependency
injection or Inversion of Control (IoC) user the same roof. A case study is presented to demonstrate the integration of three diverse technologies,
struts, hibernate and spring. JBOSS application server is employed as a container for J2EE components.
Keywords-Model-View-Controller Architecture, Loose Coupling, Object-Relation Mapping, Persistent Layer, Plain Old Java Objects, Tight
Coupling,
__________________________________________________*****________________________________________________
262
IJRITCC | March 2017, Available @ http://www.ijritcc.org
_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 5 Issue: 3 261 268
_______________________________________________________________________________________________
C. Insight into Execution of Application D. Complete Source Code
The following steps are involved in the execution of User.java
application. package com.siber;
1. The user requests login.jsp page which contains the public class User {
line String userName;
<s:form action="user"> public String message()
{
2. The struts configuration file struts.xml is looked up return null;
for action with the name user and the following line is }
encountered.
<action name="user" class="loginClass" method="execute"> public String getUserName() {
return userName;
3. The spring configuration file applicationContext.xml }
is looked up for the bean with the id loginClass and
the following line is encountered. public void setUserName(String userName) {
<bean id="loginClass" class="com.siber.Login"/> this.userName = userName;
}
4. Login class is loaded by the class loader and is }
instantiated.
Admin.java
5. SetterX() methods are invoked for initializing the package com.siber;
userName and password with the values entered by
the user in login.jsp page. public class Admin extends User{
public String message()
6. Next, Login classs execute() method is invoked. {
return "admin";
7. The first statement in execute( ) method invokes the }
static method saveUser() of UserDAO class which }
creates as object of PersistentUser class by invoking
parameterized constructor for initializing userName Guest.java
and password and employs hibernate classes for package com.siber;
persisting data into a MySQL table user.
public class Guest extends User{
8. The following statements in execute() method
dynamically instantiate UserSerive class with its public String message()
dependency Guest stored in User class reference by {
reading the corresponding information from return "guest";
applicationContext.xml file as shown below: }
<bean id="guest" class="com.siber.Guest"/> }
<bean id="admin" class="com.siber.Admin"/>
<bean id="userService" class="com.siber.UserService"> Login.java
<property name="user"> package com.siber;
<ref bean="guest"/> import
</property> org.springframework.context.support.ClassPathXmlApplicatio
</bean> nContext;
10. The struts configuration file strusts.xml is looked up public String getUserName() {
for the result equal to guest and the following line return userName;
is encountered. }
<result name="guest">/guest.jsp</result>
public void setUserName(String userName) {
11. The view specified in struts.xml file, guest.jsp is this.userName = userName;
displayed next and the response is generated to an }
end user.
public String getPassword() {
263
IJRITCC | March 2017, Available @ http://www.ijritcc.org
_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 5 Issue: 3 261 268
_______________________________________________________________________________________________
return password; this.id=id;
} }
public String getUserName() {
public void setPassword(String password) { return userName;
this.password = password; }
}
public void setUserName(String userName) {
public String execute() this.userName = userName;
{ }
try
{ public String getPassword() {
UserDAO.saveUser(userName, password); return password;
ClassPathXmlApplicationContext appContext=new }
ClassPathXmlApplicationContext( "/WEB-
INF/applicationContext.xml"); public void setPassword(String password) {
this.password = password;
UserService }
us=(UserService)appContext.getBean("userService");
//UserService us=new UserService(); }
//us.setUser(new Guest());
return us.message(); UserDAO.java
} package com.siber;
catch(Exception e) import org.hibernate.Session;
{ import org.hibernate.SessionFactory;
System.out.println(e); import org.hibernate.Transaction;
return null; import org.hibernate.cfg.Configuration;
} public class UserDAO {
public static void saveUser(String userName, String
//return "guest"; password){
}
} SessionFactory sessionFactory;
Session hibSession;
PersistentUser.java
package com.siber;
import java.io.Serializable;
sessionFactory=new
public class PersistentUser implements Serializable { Configuration().configure().buildSessionFactory();
private int id; hibSession=sessionFactory.openSession();
private String userName ; Transaction tx=null;
private String password;
try
public PersistentUser() {
{ tx=hibSession.beginTransaction();
} PersistentUser u = new
PersistentUser(userName, password);
public PersistentUser(String userName, String hibSession.save(u);
password) tx.commit();
{ }
this.userName=userName; catch(RuntimeException e)
this.password=password; {
} if (tx != null)
tx.rollback();
public int getId() throw e;
{ }
return id; }
} }
264
IJRITCC | March 2017, Available @ http://www.ijritcc.org
_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 5 Issue: 3 261 268
_______________________________________________________________________________________________
public class UserService { applicationContext.xml
User user; <?xml version="1.0" encoding="UTF-8"?>
<beans
public void setUser(User user) xmlns="http://www.springframework.org/schema/beans"
{ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
this.user=user; xmlns:p="http://www.springframework.org/schema/p"
} xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
public String message() xsi:schemaLocation="http://www.springframework.org/schem
{ a/beans
return user.message(); http://www.springframework.org/schema/beans/spring-beans-
} 2.5.xsd
} http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-
2.5.xsd
E. Structure of XML Configuration Files
http://www.springframework.org/schema/tx
web.xml http://www.springframework.org/schema/tx/spring-tx-
<?xml version="1.0" encoding="UTF-8"?> 2.5.xsd">
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema- <bean id="guest" class="com.siber.Guest"/>
instance" xmlns="http://java.sun.com/xml/ns/javaee" <bean id="admin" class="com.siber.Admin"/>
xmlns:web="http://java.sun.com/xml/ns/javaee/web- <bean id="userService" class="com.siber.UserService">
app_2_5.xsd" <property name="user">
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee <ref bean="guest"/>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" </property>
id="WebApp_ID" version="2.5"> </bean>
<listener> <bean id="loginClass" class="com.siber.Login"/>
<listener-class> </beans>
org.springframework.web.context.ContextLoaderListener
</listener-class> PersistentUser.hbm.xml
</listener> <?xml version="1.0" encoding="UTF-8"?>
<filter> <!DOCTYPE hibernate-mapping PUBLIC "-
<filter-name>struts2</filter-name> //Hibernate/Hibernate Mapping DTD 3.0//EN"
<filter-class> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecut <hibernate-mapping>
eFilter <class name="com.siber.PersistentUser" table="login"
</filter-class> catalog="test">
</filter> <id name="id" type="java.lang.Integer">
<filter-mapping> <column name="id"/>
<filter-name>struts2</filter-name> <generator class="identity"/>
<url-pattern>/*</url-pattern> </id>
</filter-mapping> <property name="userName" type="java.lang.String">
</web-app> <column name="username" length="50"/>
</property>
struts.xml <property name="password" type="java.lang.String">
<?xml version="1.0" encoding="UTF-8"?> <column name="Password" length="50"/>
<!DOCTYPE struts PUBLIC </property>
"-//Apache Software Foundation//DTD Struts Configuration </class>
2.0//EN" </hibernate-mapping>
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts> hibernate.cfg.xml
<constant name="struts.devMode" value="true" /> <?xml version="1.0" encoding="UTF-8"?>
<package name="helloworld" extends="struts-default"> <!DOCTYPE hibernate-configuration PUBLIC "-
<action name="user" class="loginClass" //Hibernate/Hibernate Configuration DTD 3.0//EN"
method="execute"> "http://hibernate.sourceforge.net/hibernate-configuration-
<result name="admin">/admin.jsp</result> 3.0.dtd">
<result name="guest">/guest.jsp</result> <hibernate-configuration>
</action> <session-factory>
</package>
</struts>
265
IJRITCC | March 2017, Available @ http://www.ijritcc.org
_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 5 Issue: 3 261 268
_______________________________________________________________________________________________
<property </html>
name="connection.driver_class">com.mysql.jdbc.Driver</pro
perty> guest.jsp
<property <%@ page language="java" contentType="text/html;
name="connection.url">jdbc:mysql://localhost:3306/test</pro charset=ISO-8859-1"
perty> pageEncoding="ISO-8859-1"%>
<property name="connection.username">root</property> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
<property name="connection.password">mca</property> Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<property <html>
name="dialect">org.hibernate.dialect.MySQLDialect</propert <head>
y> <meta http-equiv="Content-Type" content="text/html;
<mapping resource="com/siber/PersistentUser.hbm.xml" /> charset=ISO-8859-1">
</session-factory> <title>Insert title here</title>
</hibernate-configuration> </head>
<body>
F. Client Side Code
<h1><font color=blue>Welcome Guest </font></h1>
login.jsp <table border width="80%">
<%@ page language="java" contentType="text/html; <tr>
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <td><a href="">Browse Products</a></td>
<%@ taglib prefix="s" uri="/struts-tags" %> <td><a href="">Sign Out</a></td>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 </tr>
Transitional//EN" </table>
"http://www.w3.org/TR/html4/loose.dtd"> </body>
<html> </html>
<head>
<title>Hello World</title> IV. RESULTS AND DISCUSSIONS
</head>
A. Testing of the Application
<body>
<h1>Struts2 - Spring and Hibernate Integration</h1> The user generates an HTTP request, requesting the login.jsp
<s:form action="user"> page. The HTTP response generated by JBOSS application
<s:textfield name="userName" label="User Name"/><br/> server is depicted in Figure
<s:password name="password" label="Password"/><br/>
<center><s:submit value="Login"/></center>
</s:form>
</body>
</html>
admin.jsp
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> Figure 4. End User Requesting Login Page
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> The hibernate framework constituting the persistent layer of
<title>Insert title here</title> the application retrieves the data input by the user and inserts a
</head> corresponding record in login table of MySQL database as
<body> shown in Figure 5.
<h1><font color=green>Admin Console</font></h1>
<table border width="80%">
<tr>
<td><a href="">User Maintenance</a></td>
<td><a href="">Stock Maintenance</a></td>
<td><a href="">View Reports</a></td>
<td><a href="">Sign In</a></td>
</tr>
</table>
</body>
266
IJRITCC | March 2017, Available @ http://www.ijritcc.org
_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 5 Issue: 3 261 268
_______________________________________________________________________________________________
In the case of tight-coupling, all the objects along with its all
dependencies are created in the code. In contrast to this in the
case of loose coupling, the dependency information is stored
in a spring configuration file and when the application requests
the container for the object containing some dependencies, the
container instantiates the object along with its all dependencies
dynamically at runtime. The technique is referred to as
Dependency injection or Inversion of Control, IoC.
268
IJRITCC | March 2017, Available @ http://www.ijritcc.org
_______________________________________________________________________________________