Step1:Develop a jsp file index.jsp
<%@taglib uri="http://struts.apache.org/tags-html" prefix="h"%>


    <h:link href="create.jsp">Create</h:link>
    <h:link href="delete.jsp">Delete</h:link>
    <h:link href="search.jsp">Search</h:link>



step2: develop a struts Action class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.bcns.action;

import info.bcns.dao.AllDao;
import info.bcns.dao.UserSearchDTO;
import info.bcns.javabean.ProductJB;
import info.bcns.mapping.Product;
import info.bcns.template.HibernateSessionFactory;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.validator.DynaValidatorForm;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class AllProductDetails extends DispatchAction {

    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";

    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       
        String pid1=request.getParameter("pid");
        int pid=Integer.parseInt(pid1);
        String pname=request.getParameter("pname");
        String price1=request.getParameter("price");
        int price=Integer.parseInt(price1);
     
     SessionFactory sf=HibernateSessionFactory.getSessionFactory();
      Session s= sf.openSession();
      Transaction t= s.beginTransaction();
      Product p=new Product();
       p.setPid(pid);
        p.setPname(pname);
        p.setPrice(price);
      s.save(p);
      t.commit();
      
        /*Product p=(Product)form;
        p.setPid(Integer.parseInt("pid"));
        p.setPname("pname");
        p.setPrice(Integer.parseInt("price"));
        ProductJB pjb=new ProductJB();
        pjb.insert(p);
         */

        request.setAttribute("msg","successfully inserted");
        return mapping.findForward(SUCCESS);
  /* }else{
    return mapping.findForward("FAILURE");
   }*/
    }

  
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       
        String pid1=request.getParameter("pid");
        int pid=Integer.parseInt(pid1);
       SessionFactory sf=HibernateSessionFactory.getSessionFactory();
      Session s= sf.openSession();
      Transaction t= s.beginTransaction();
     
      //Another way for deleteing records from Db using Hql Query
     // String query="delete from Product where pid="+pid;
     
    Query q=s.createQuery("delete from info.bcns.mapping.Product where pid=?");
    q.setInteger(0,pid);
      q.executeUpdate();
     
      /* Product p=new Product();
      p.setPid(pid);    //this is for single row operation deleting records from db
      s.delete(p);
      
     */
      t.commit();
       
        request.setAttribute("msg","successfully deleted record from Db");
        return mapping.findForward(SUCCESS);
    }
   
   
    public ActionForward search(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
      
       // UserSearchDTO usdto=(UserSearchDTO)form;
      // usdto.getPid();
      
       
        DynaValidatorForm dvf = (DynaValidatorForm)form;
       UserSearchDTO sudto = new UserSearchDTO();
      
    
             sudto.setPid((String)dvf.get("pid"));
       AllDao ad=new AllDao();
       ArrayList list=ad.search(sudto);
       
      request.setAttribute("list",list);
         
        request.setAttribute("msg","Available Records are..");
        return mapping.findForward("display");
    }
   
   
}


step3: develop a form bean class

package info.bcns.mapping;
// Generated Sep 24, 2013 8:24:03 AM by Hibernate Tools 3.2.1.GA

/**
 * Product generated by hbm2java
 */
public class Product {


     private int pid;
     private String pname;
     private Integer price;

    public Product() {
    }

   
    public Product(int pid) {
        this.pid = pid;
    }
    public Product(int pid, String pname, Integer price) {
       this.pid = pid;
       this.pname = pname;
       this.price = price;
    }
  
    public int getPid() {
        return this.pid;
    }
   
    public void setPid(int pid) {
        this.pid = pid;
    }
    public String getPname() {
        return this.pname;
    }
   
    public void setPname(String pname) {
        this.pname = pname;
    }
    public Integer getPrice() {
        return this.price;
    }
   
    public void setPrice(Integer price) {
        this.price = price;
    }
  
}

step4:develop a .Product.hbm.xml file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 26, 2013 10:43:59 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="info.bcns.mapping.Product" table="product" catalog="ashok">
        <id name="pid" type="int">
            <column name="pid" />
            <generator class="assigned" />
        </id>
        <property name="pname" type="string">
            <column name="pname" length="23" />
        </property>
        <property name="price" type="java.lang.Integer">
            <column name="price" />
        </property>
    </class>
</hibernate-mapping>

step5: develop hibernate.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
  <schema-selection match-catalog="ashok"/>
  <table-filter match-name="product"/>
</hibernate-reverse-engineering>


step6:develop a hibernate.cfg.xml file

<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ashok</property>
    <property name="hibernate.connection.username">root</property>
    <mapping resource="info/bcns/mapping/Product.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
 step7: develop a HibernateSessionFactory.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.bcns.template;


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

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author user
 */
public class HibernateSessionFactory {

    private static SessionFactory sf=null;
   
    static{
        sf=new Configuration().configure().buildSessionFactory();
       
    }
    public static SessionFactory getSessionFactory(){
       
        return sf;
    }
   
    public HibernateSessionFactory(){
       
       
    }

    public Session openSession() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}


step8: HibernateTemplate.java file
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.bcns.template;

import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateTemplate {
   
    public static void save(Object o){
      SessionFactory sf=HibernateSessionFactory.getSessionFactory();
      Session s=sf.openSession();
      Transaction t=s.beginTransaction();
      s.save(o);
      t.commit();
      s.close();
    }
   
    public static  ArrayList getAllRecords(String hqlQuery) {
 
    SessionFactory sf = HibernateSessionFactory.getSessionFactory();
    Session hsession = sf.openSession();
    Query query  = hsession.createQuery(hqlQuery);
    ArrayList list  = (ArrayList)query.setFirstResult(0).setMaxResults(3).list();
    hsession.close();
    return list;
}

   
}




step9: AllDao.java file
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.bcns.dao;

import info.bcns.mapping.Product;
import info.bcns.template.HibernateSessionFactory;
import info.bcns.template.HibernateTemplate;
import java.util.ArrayList;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author user
 */
public class AllDao {
    public static boolean insertRecord(Product p){
     HibernateTemplate.save(p);
    return true;
    }
   
   public ArrayList search(UserSearchDTO srdto) {

String hqlQuery = "from info.bcns.mapping.Product";
if(!srdto.getPid().equals("")||srdto.getPid()==null) {
hqlQuery=hqlQuery+" where pid="+ "\'"+srdto.getPid()+"\'";
}

return HibernateTemplate.getAllRecords(hqlQuery);
}

}


step10: for searching records we can take UserSearchDTO.java file
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.bcns.dao;

/**
 *
 * @author user
 */
public class UserSearchDTO{
   
   String pid;

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

}
stpe11:display.jsp file
<%@ page isELIgnored="false"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
Hi <font color="red" align="right"><i><%=request.getAttribute("msg")%></i></font>
<br>

<h:link href="create.jsp">Create</h:link>
    <h:link href="delete.jsp">Delete</h:link>
    <h:link href="search.jsp">Search</h:link>
<html:form action="a.do?ashok=search">

    Product ID:<html:text property="pid"/><br><br>
   
    <html:submit value="Search"/>
</html:form>
<table border="1">
 <tr>
    <th>Pid:</th>
    <th>PName:</th>
    <th>Price:</th>
</tr>
<logic:empty name="list">
    <tr>
    <td><font color="blue">No Records Found</font></td>
</tr>
</logic:empty>
<logic:iterate id="user" name="list">
  <tr>    
<td><bean:write name="user" property="pid"/></td>
<td><bean:write name="user" property="pname"/></td>
<td><bean:write name="user" property="price"/></td>
<td><a href="delete.jsp?pid=<%=("pid")%>">Delete</a></td>
<td><a href="create.jsp">Create</a></td>
</tr>


</logic:iterate>
</table><br>
<a href="search.jsp">Search</a><br><br>

step12:success.jsp file
Hi <font color="red"><i><%=request.getAttribute("msg")%></i></font><br><br>

<a href="index.jsp">Home</a>

step14:
create.jsp
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html:form action="a.do?ashok=create">

    Product ID:<html:text property="pid"/><br><br>
    Product Name:<html:text property="pname"/><br><br>
    Product price:<html:text property="price"/><br><br>
    <html:submit value="Add"/>
</html:form>


step15:
search.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<a href="index.jsp" align="left"><image src="./images/h.jpg" alt="Home"/></a><br>

<html:form action="a.do?ashok=search">

    Product ID:<html:text property="pid"/><br><br>
   
    <html:submit value="Search"/>
</html:form>


step16:delete.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html:form action="a.do?ashok=delete">

    Product ID:<html:text property="pid"/><br><br>
   
    <html:submit value="Delete"/>
</html:form>


































 Download Application with jar files DispatchAction Code using pagination Code(Mysql)   

0 comments:

Post a Comment