Struts Spring Hibernate

In this section, you will learn about the Struts, Spring and Hibernate Integration.

Struts Spring Hibernate

--Ads--

Struts Spring Hibernate

In this section, you will learn about the Struts, Spring and Hibernate Integration.

The tools & technology used is given below :

  • struts 2.1.8

  • spring 2.5.6

  • hibernate 3.2.7

  • dom4j 1.6.1

  • cglib 2.2

  • Eclipse 3.5

The SQL query used to create the employee table is given below :

CREATE TABLE `employee` ( 
	`EMPLOYEE_ID` bigint(20) unsigned NOT NULL auto_increment, 
	`NAME` varchar(45) NOT NULL, 
	`ADDRESS` varchar(255) NOT NULL, 
	`CREATED_DATE` datetime NOT NULL, 
	PRIMARY KEY (`EMPLOYEE_ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

Three easy steps to integrate Struts, Spring and Hibernate :

  • Collect all the jar file needed into project lib.

  • To integrate Struts 2 with Spring, incorporate ContextLoaderListener.

  • To integrate Spring with Hibernate, incorporate LocalSessionFactoryBean.

Let us discuss these steps in detail using a example :

Example

The project structure is given below:

The jar file used is given below :

CODES

web.xml

In the below example, Listener class ContextLoaderListener loads SpringBeans.xml file(which load beans), and also interact with Struts. It also loads bean class LocalSessionFactoryBean which will deal with Hibernate.

<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>StrutsSpringHibernate</display-name>
<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>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringBeans.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>

SpringBeans.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Database Configuration -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/properties/database.properties
</value>
</property>
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">
<ref bean="dataSource"/>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>

<property name="mappingResources">
<list>
<value>net/roseindia/hibernate/Employee.hbm.xml</value>
</list>
</property>
</bean>
<!-- Beans Declaration -->
<bean id="employeeAction" class="net.roseindia.employee.action.EmployeeAction">
<property name="employeeDAO" ref="employeeDAO" />
</bean>

<bean id="employeeDAO" class="net.roseindia.employee.dao.impl.EmployeeDAOImpl" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

struts.xml

<?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.devMode" value="true" />

<package name="default" extends="struts-default" namespace="/" >

<action name="addEmployeeAction" 
class="net.roseindia.employee.action.EmployeeAction" method="addEmployee" >
<result name="success">pages/employee.jsp</result>
</action>

<action name="listEmployeeAction"
class="net.roseindia.employee.action.EmployeeAction" method="listEmployee" >
<result name="success">pages/employee.jsp</result>
</action>
</package>

</struts>

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="net.roseindia.employee.model.Employee" 
table="employee">
<id name="employeeId" type="java.lang.Long">
<column name="EMPLOYEE_ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" length="45" not-null="true" />
</property>
<property name="address" type="string">
<column name="ADDRESS" not-null="true" />
</property>
<property name="createdDate" type="timestamp">
<column name="CREATED_DATE" length="19" not-null="true" />
</property>
</class>
</hibernate-mapping>

Employee.java

package net.roseindia.employee.model;

import java.util.Date;

public class Employee implements java.io.Serializable {

private Long employeeId;
private String name;
private String address;
private Date createdDate;

public Employee() {
}

public Employee(Long employeeId, String name, String address,
Date createdDate) {
this.employeeId = employeeId;
this.name = name;
this.address = address;
this.createdDate = createdDate;
}

public Long getEmployeeId() {
return employeeId;
}

public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Date getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}

}

EmployeeDAO.java

package net.roseindia.employee.model;

import java.util.Date;

public class Employee implements java.io.Serializable {

private Long employeeId;
private String name;
private String address;
private Date createdDate;

public Employee() {
}

public Employee(Long employeeId, String name, String address,
Date createdDate) {
this.employeeId = employeeId;
this.name = name;
this.address = address;
this.createdDate = createdDate;
}

public Long getEmployeeId() {
return employeeId;
}

public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Date getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}

}

EmployeeDAOImpl.java

package net.roseindia.employee.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import net.roseindia.employee.dao.EmployeeDAO;
import net.roseindia.employee.model.Employee;

public class EmployeeDAOImpl extends HibernateDaoSupport implements EmployeeDAO{

//add the customer
public void addEmployee(Employee employee){

getHibernateTemplate().save(employee);

}

//return all the customers in list
public List<Employee> listEmployee(){

return getHibernateTemplate().find("from Employee");

}

}

EmployeeAction.java

package net.roseindia.employee.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.roseindia.employee.dao.EmployeeDAO;
import net.roseindia.employee.model.Employee;
import com.opensymphony.xwork2.ModelDriven;

public class EmployeeAction implements ModelDriven{

Employee employee = new Employee();
List<Employee> employeeList = new ArrayList<Employee>();

EmployeeDAO employeeDAO;

//Spring Dependency Injection
public void setEmployeeDAO(EmployeeDAO employeeDAO) {
this.employeeDAO = employeeDAO;
}

public Object getModel() {
return employee;
}

public List<Employee> getEmployeeList() {
return employeeList;
}

public void setEmployeeList(List<Employee> employeeList) {
this.employeeList = employeeList;
}

//saving employee record
public String addEmployee() throws Exception{ 
//save employee
employee.setCreatedDate(new Date());
employeeDAO.addEmployee(employee);

//reload the employee list
employeeList = null;
employeeList = employeeDAO.listEmployee();

return "success";
}

//list all customers
public String listEmployee() throws Exception{
employeeList = employeeDAO.listEmployee();
return "success";
}

}

database.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.10.13:3306/company
jdbc.username=root
jdbc.password=root

employee.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h2><i>Integration Example - Struts 2,Spring & Hibernate</i></h2>
<hr></hr>
<h3><i>Add New Employee</i></h3>
<s:form action="addEmployeeAction" >
<s:textfield name="name" label="Employee Name" value="" size="37"/>
<s:textarea name="address" label="Employee Address" value="" cols="28" rows="3" />
<s:submit align="center"/>
</s:form>
<hr></hr>
<h3><i>Employee List</i></h3>

<s:if test="employeeList.size() > 0">
<table border="2px" cellpadding="8px">
<tr>
<th>Employee<br/>ID</th>
<th>Name</th>
<th>Address</th>
<th>Created Date</th>
</tr>
<s:iterator value="employeeList" status="userStatus">
<tr>
<td><s:property value="employeeId" /></td>
<td><s:property value="name" /></td>
<td><s:property value="address" /></td>
<td><s:date name="createdDate" format="dd/MM/yyyy" /></td>
</tr>
</s:iterator>
</table>
</s:if>
<br/>
<br/>

</body>
</html>

OUTPUT

When you call the following URL :

http://localhost:9090/StrutsSpringHibernate/listEmployeeAction.action

you will get the following page :

Download Source Code