Thursday 16 October 2014

Struts2 Hibernate Integration java programmer can use it in simplified manner


 Create A Model Class  A class:

package Model;

public class Emp
{

String name,job;
int salary;
int id;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(String name, String job, int salary) {
super();
this.name = name;
this.job = job;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}

Create Hibernate Session Factory

package HibernateUtile;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class MySesssionFactory
{
static SessionFactory factory;
static
{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
public static Session getSession()
{
return factory.openSession();
}
}


Create Dao Class

package Dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import HibernateUtile.MySesssionFactory;
import Model.Emp;

public class EmpDao
{
public void insert(Emp emp)
{
Session session=MySesssionFactory.getSession();
Transaction tr=session.beginTransaction();
session.save(emp);
tr.commit();
session.close();
}
}


Create Hibernate Configuration


 <?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="connection.username">system</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.password">oracle</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="show_sql">true</property>
    <mapping resource="Hibernate.hbm.xml"/>
    </session-factory>

</hibernate-configuration>



Create Hibernate Mapping File

<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-mapping>
 <class name="Model.Emp">
    <id name="id" type="int">
    <generator class="increment"/>
    </id>
    <property name="name"/>
    <property name="job"/>
    <property name="salary" type="int"/>

</class>
</hibernate-mapping>



Create Controller Page

package View;

import Dao.EMpDao;
import Model.Emp;

public class ActionAaAview
{
private Emp e;

public Emp getE() {
return e;
}

public void setE(Emp e) {
this.e = e;
}
public String execute()
{
EmpDao ctr=new EmpDao();
ctr.insert(e);
return "success";
}
}



Create struts.xml file

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

<struts>
<package name="demo" extends="struts-default">
<action name="add" class="View.ActionAaAview">
<result name="success">/Welcome.jsp</result>
<!--<result name="failure">/Retrive.jsp</result>-->
</action>
</package>
</struts>



Copy bellow text and paste in web.xml file


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


Create index.jsp

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

<s:form action="add" >

<s:textfield name="Emp.name" label="Name"/>
<s:textfield name="Emp.job" label="Job"/>
<s:textfield name="Emp.salary" label="Salary"/>
<s:token/>
<s:submit name="Register"/>
<s:reset name="Reset"/>


</s:form>



Create welcome.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
Welcome<s:property value="Emp.name"/>

</s:form>


Create retrive.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="add" method="post">
<s:textfield name="name" label="Name"/>
<s:textfield name="job" label="Job"/>
<s:textfield name="salary" label="Salary"/>
<s:submit name="Register"/>
<s:reset name="Reset"/>
<s:textfield />

</s:form>



No comments:

Post a Comment