JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Struts2-Spring-Hibernate Integration

We can integrate all three technology into one.

Example::-
  • The following example show the strut2-spring-hibernate integrated Application.
  • The following  Example show all the Employees in the database in a jsp file.
Required Liabraires File

  •  From Hibernate all needed jar files.
  •  From Struts2 1
    •  commons-fileupload-1.2.1 2
    •  commons-io-1.3.2 3
    •  freemarker-2.3.15 4
    •  ognl-2.7.3
  • struts2-core-2.1.8.1 6
    •  xwork-core-2.1.6 7
    •  struts2-spring-plugin.jar
  •  from Spring2.5,which is required for Struts2.1
    • spring-context-2.5.6.jar 
    •  spring-core-2.5.6.jar
    • spring-jdbc.jar
    • spring.orm.jar 
    • spring-tx.jar 
    • spring-web.jar
    • spring-aop.jar 
    • spring-beans.jar
  • mysql driver


index.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<html>

<body>
  <h3>Welcome to Employee Information Page</h3>
  <br>

  <s:form action="showEmp">
    <s:submit value="Show Records" />
  </s:form>
</body>
</html>


struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software
Foundation//DTD Struts
Configuration 2.0//EN" "http://struts.apache.
org/dtds/struts-2.0.dtd">

<struts>
 <package name="default" extends="struts-default">
 
 <action name="showEmp" class="myaction.EmployeeAction">
  <result name="success">/success.jsp</result>
 </action>
 </package>
</struts>


EmployeeAction.java
package myaction;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import myDao.*;
import myentity.Employee;

public class EmployeeAction extends ActionSupport
 
implements ModelDriven {

  private Employee emp;
  private List emplist = new ArrayList();
  private EmployeeDaoImp empdao;

  public void setEmpdao(EmployeeDaoImp empdao) {
    this.empdao = empdao;
  }

  @Override
  public Employee getModel() {
    return emp;
  }

  @Override
  public String execute() throws Exception {
    emplist = empdao.showEmployuee();
    return SUCCESS;

  }

  public Employee getEmp() {
    return emp;
  }

  public void setEmp(Employee emp) {
    this.emp = emp;
  }

  public List getEmplist() {
    return emplist;
  }

  public void setEmplist(List emplist) {
    this.emplist = emplist;
  }

}

EmployeeDao.java
package myDao;

import java.util.List;

interface EmployeeDao {

  List showEmployuee();
}

EmployeeDaoImp.java
package myDao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class EmployeeDaoImp implements EmployeeDao {

  private SessionFactory sessionfactory;

  public void setSessionfactory(SessionFactory sessionfactory) {
    this.sessionfactory = sessionfactory;
  }

  @Override
  public List showEmployuee() {

    Session session = null;

    session = sessionfactory.openSession();
    List emp = null;
    try {
      emp = session.createQuery("from Employee").list();
    catch (Exception e) {
      e.printStackTrace();
    }
    return emp;
  }

}

HibernateMapping.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<hibernate-mapping>
  <class name="myentity.Employee" table="EMPLOYEE">
    <id name="empid">
      <column name="EMP_ID" />
      <generator class="assigned" />
    </id>
    <property name="empname">
      <column name="EMP_NAME" />
    </property>
    <property name="salary">
      <column name="EMP_SAL" />
    </property>
  </class>
  
</hibernate-mapping>

SpringConfiguration.xml
<?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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org
/schema/beans http://www.springframework.org/schema/
beans/spring-beans-2.5.xsd"
>

  <bean id="springdatasource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
  </bean>
  
  <bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="springdatasource" />
<property name="mappingResources">
<list>
<value>HibernateMapping.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=
org.hibernate.dialect.MySQLDialect</value>

</property>
</bean>

<bean id="empdao" class="myDao.EmployeeDaoImp">
<property name="sessionfactory" ref="sessionFactory"/>
</bean>

<bean id="empAction" class="myaction.EmployeeAction"
<property name="empdao" ref="empdao"/>
</bean>
  
</beans>

web.xml
<?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/w
eb-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/SpringConfiguration.xml
</param-value>

  </context-param>

<filter>
      <filter-name>Struts2Filter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-
class>
    </filter>
    <filter-mapping>
      <filter-name>Struts2Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>

</listener>
  <welcome-file-list>
    
    <welcome-file>index.jsp</welcome-file>
  
  </welcome-file-list>
</web-app>

success.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<html>

<body>
  <h3>Welcome to Employee Information Page</h3>
  <br>
  <table>
    <tr>
      <th>EMP_ID</th>
      <th>EMP_NAME</th>
      <th>EMP_SAL</th>
    </tr>
    <s:iterator value="emplist">
      <tr>
        <td><s:property value="empid" /></td>
        <td><s:property value="empname" /></td>
        <td><s:property value="salary" /></td>
      </tr>
    </s:iterator>
  </table>

</body>
</html>

Output:




Download Source without lib
Download War with lib