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







Struts 2 Hibernate Integration
  • Struts 2 and Hibernate Integration can be done using the "Full
    Hibernate Plugin.
  • To use the Full Hibernate Plugin 1.4 GA you need to add the following lib files to the lib
    directory.


Example:- The following  Example show all the Employees in the database in a jsp file.


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="hibernate-default">
 
 <action name="showEmp" class="myaction.EmployeeAction">
  <result name="input">/input.jsp</result>
  <result name="success">/success.jsp</result>
 </action>
 </package>
</struts>


Employee.java
package myentity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

public class Employee {

  private Long empid;
  private String empname;
  private int salary;

  public Long getEmpid() {
    return empid;
  }

  public void setEmpid(Long empid) {
    this.empid = empid;
  }


  public String getEmpname() {
    return empname;
  }

  public void setEmpname(String empname) {
    this.empname = empname;
  }


  public int getSalary() {
    return salary;
  }

  public void setsalary(int salary) {
    this.salary = salary;
  }
}

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 {

  Session session = null;

  @Override
  public List showEmployuee() {
    Configuration cfg = new Configuration();
    cfg.configure("HibernateConfiguration.cfg.xml");
    SessionFactory sessionfactory = cfg.buildSessionFactory();
    session = sessionfactory.openSession();
    List emp = null;
    try {
      emp = session.createQuery("from Employee").list();
    catch (Exception e) {
      e.printStackTrace();
    }
    return emp;
  }

}

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 = new Employee();
  private List emplist = new ArrayList();
  private EmployeeDaoImp empDAO = new EmployeeDaoImp();

  @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;
  }

}

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>

HibernateConfiguration.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-
configuration-3.0.dtd"
>
<hibernate-configuration>
    <session-factory>
      <property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver</property>

      <property name="hibernate.connection.url">
jdbc:oracle:thin:
@localhost:1521:XE</property>
      <property name="hibernate.connection.username">
system</property>

      <property name="hibernate.connection.password">
manager</property>

      <property name="hibernate.show_sql">true</property>
      
      <property name="hibernate.dialect">o
rg.hibernate.dialect.OracleDialect</property>

      
       <mapping resource="HibernateMapping.hbm.xml" />
       
    </session-factory>
</hibernate-configuration>


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>

  <class name="myentity.Hr" table="hr_information">
    <id name="id">
      <column name="hr_id" />
      <generator class="assigned" />
    </id>
    <property name="firstname">
      <column name="hr_fname" />
    </property>
    <property name="lastname">
      <column name="hr_lname" />
    </property>
    <property name="dateofjoining">
      <column name="hr_doj" />
    </property>
    <property name="username">
      <column name="hr_uname" />
    </property>
    <property name="password">
      <column name="hr_pwd" />
    </property>
  </class>
</hibernate-mapping>


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/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
 

<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>
    
  <welcome-file-list>
    
    <welcome-file>index.jsp</welcome-file>
  
  </welcome-file-list>
</web-app>


Output:-






Download Source without lib
Download  War with lib