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-Hibernate Integration
  • Struts and Hibernate Integration can be done
  • Required all needed hibernate jar file.
Example:
  • This example take the book id nad book name from the user.
  • Using hibernate we are inserting the records into the table in the database.
  • This Example uses the Hibernate Annotation machanism.

index.jsp
<%taglib uri="http://jakarta.apache.org/struts/tags-html"
  prefix="html"%>
  <h3>struts-hibernate Integration Example</h3>
<html:form action="/go">
id....... <html:text property="id" />
  <br>
name  <html:text property="name" />
  <br>

  <html:submit />
  <br>
</html:form>

welcome.jsp

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

Id is
<bean:define id="i" property="id" name="book" />
<bean:define id="na" property="name" name="book" /><%=i%>
<br>
name is ....<%=na%>
<br>
<b>Record Inserted</b>

<!-- id -->
<%-- <bean:write name="book" property="id" scope="session" /> --%>
<!-- and name -->
<%-- <bean:write name="book" property="name" scope="session"/> --%>
<!-- inserted -->

struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration
 1.1//EN" "http:/
/jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"
>

<struts-config>

  <form-beans>
    <form-bean name="book" type="pack.book" />
  </form-beans>
  <action-mappings>
    <action path="/go" type="pack.bookAction" name="book">
      <forward name="success" path="/welcome.jsp" />
      <forward name="failure" path="/index.jsp" />
    </action>

  </action-mappings>
  <message-resources parameter="pack/message" />
</struts-config>

bookAction.java
package pack;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class bookAction extends Action{
  
  @Override
  public ActionForward execute(ActionMapping mapping,
ActionForm form,

      HttpServletRequest request,
HttpServletResponse response
throws Exception {
    
    String target="success";
    
     book buk=(book)form;
     
     if(new Integer(buk.getId()).toString()=="" )
       target="failure";
     
     if(buk.getName()=="" )
       target="failure";
     else{
       
       SessionFactory fact =
new AnnotationConfiguration().configure()
.buildSessionFactory();
            

        Session session1 = fact.openSession();
        session1.getTransaction().begin();
        session1.save(buk);
        session1.getTransaction().commit();
       
     }
    return mapping.findForward(target);
  }
  
}

book.java
package pack;

import javax.persistence.Entity;
import javax.persistence.Id;

import org.apache.struts.action.ActionForm;

@Entity
public class book extends ActionForm {
  private int id;
  private String name;

  @Id
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

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

}

hibernate.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">
com.mysql.jdbc.Driver</property>


    <property name="hibernate.connection.url">
jdbc:mysql:
//localhost:3306/test</property>
    <property name="hibernate.connection.username">
root</property>

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

    <property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect</property>


    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>

    <mapping class="pack.book" />


    <!-- <mapping resource="counter.hbm.xml"/> -->


  </session-factory>
</hibernate-configuration>

Output:-
  • Run the index.jsp page as shown below
  • when the user enter book id and book name as shown below.
  • the record will be inserted successfully in the table.




Download Eclipse Code without lib

Download code with lib(WAR)



Example:- Dispatch Action With DBCP CRUD Application

  • Insert Operation
  • Update Operation
  • Delete Operation
dataAdd.jsp
<%taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:form action="/data">
Id  <html:text property="id" />

  <br>
  <br>
Name <html:text property="name" />
  <br>
  <br>

Select Opearation
<br>
  <br>
<html:radio property="method" value="delete" />Delete
<br>

  <html:radio property="method" value="update" />Update
<br>

  <html:radio property="method" value="insert" />Insert
<html:errors />
  <br>
  
  <html:submit />

</html:form>


book.java

package data;

import javax.persistence.Entity;
import javax.persistence.Id;

import org.apache.struts.action.ActionForm;

@Entity
public class book extends ActionForm {
  private int id;
  private String name;

  @Id
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

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

  private String method;

  public String getMethod() {
    return method;
  }

  public void setMethod(String method) {
    this.method = method;
  }

}

BookAction.java
package data;

import java.sql.Connection;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class BookAction extends DispatchAction {

  String target;
  SessionFactory sessionfactory = new
 
AnnotationConfiguration().configure().buildSessionFactory();
  Session session=null;

  public ActionForward delete(ActionMapping mapping,
 ActionForm form,

      HttpServletRequest request, HttpServletResponse response)
      throws Exception {

    book buk = (bookform;
    
    String method = buk.getMethod();

    if (buk.getId() <= 0)
      target = "cancel";

    

    else if (method.equals(""))
      target = "cancel";
  else {
  
      session = sessionfactory.openSession();
      session.getTransaction().begin();
      
      buk=(book)session.get(book.class, buk.getId());
      
      session.delete(buk);
      session.getTransaction().commit();
      session.close();
      sessionfactory.close();
        target = "ok";
    
  }
    return mapping.findForward(target);
  }

  public ActionForward update(ActionMapping
mapping, ActionForm form,

      HttpServletRequest request,
 HttpServletResponse response
){
      book buk = (bookform;
      
      String method = buk.getMethod();

      if (buk.getId() <= 0)
        target = "cancel";

      else if (buk.getName()== "")
        target = "cancel";

      else if (method.equals(""))
        target = "cancel";
    else {
    
        session = sessionfactory.openSession();
        session.getTransaction().begin();
        String newname=buk.getName();
        buk=(book)session.get(book.class, buk.getId());
        buk.setName(newname);
        session.save(buk);
        session.getTransaction().commit();
        session.close();
        sessionfactory.close();
      target = "ok";
      
    }

    return mapping.findForward(target);
  }

  public ActionForward insert(ActionMapping
mapping, ActionForm form,

      HttpServletRequest request,
 HttpServletResponse response
)
      throws Exception {
    book buk = (bookform;
    
    String method = buk.getMethod();

    if (buk.getId() <= 0)
      target = "cancel";

    else if (buk.getName()== "")
      target = "cancel";

    else if (method.equals(""))
      target = "cancel";
    else {
       
        

        session = sessionfactory.openSession();
        session.getTransaction().begin();
        session.save(buk);
        session.getTransaction().commit();
        session.close();
        sessionfactory.close();
      target = "ok";
    }

    return mapping.findForward(target);
  }

}

message.properties
error.name=<br><font size="3" color="red">
First name cannot be blank</font><br>

error.id=<br><font size="3" color="red">
Id cannot be blank</font><br>

error.method=<br><font size="3" color="red">
Please Select At Least One  Method </font><br>


hibernate.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">
com.mysql.jdbc.Driver</property>


    <property name="hibernate.connection.url">
jdbc:mysql:
//localhost:3306/test</property>
    <property name="hibernate.connection.username">
root</property>

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

    <property name="hibernate.dialect"
>org.hibernate.dialect.MySQLDialect</property>


    <property name="show_sql">true</property>
    <!-- <property name="hbm2ddl.auto">create</property> -->

    <mapping class="data.book" />


    <!-- <mapping resource="counter.hbm.xml"/> -->


  </session-factory>
</hibernate-configuration>

struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//
DTD Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts
/dtds/struts-config_1_2.dtd"
>

<struts-config>

  
  <form-beans>

    <form-bean name="dform" type="data.book" />

  </form-beans>

  <action-mappings>

    <action type="data.BookAction" name="dform"
 
path="/data" scope="session"
      parameter="method" input="/dataAdd.jsp">

      <forward name="ok" path="/dataDisp.jsp" />
      <forward name="cancel" path="/dataAdd.jsp" />

    </action>

  </action-mappings>

  <message-resources parameter="message" />

</struts-config>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.
com/xml/ns/j2ee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml
/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet
</servlet-
class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml
</param-value>

    </init-param>
  
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
           dataAdd.jsp
        </welcome-file>
  </welcome-file-list>

</web-app>


Output:-
  • When we run  dataAdd.jsp and Enter both the id and name.
  • If we select  insert operation it will take both the id and name of the book  insert the same into
    the book table in the database.
  • As shown below.






  • Here Again if  we select Update operation
    it will  replace the name with Java as before dot net of id =1001.
  • As shown below.





  • If we select Delete, then it will delete the record of id=1001 from the book table in database
  • As shown below.






Download Eclipse Code without lib

Download code with lib(WAR)