Sunday 10 July 2016

STRUTS 2 HIBERNATE INTEGRATION EXAMPLE

The following software are used :

Steps for integration on  Struts2 and Hibernate


Step 1: Create homepage
Step 2: Create struts and web xml file
Step 3: Create Action/POJO class and mapping file
Step 4: Create hibernate.cfg.xml &hbm file
Step 5: Create success and failure page

Step1: home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form name="frm" action="reg">
    Name<s:textfield name="name"/>
    Password<s:password name="pwd"/>
    City<s:textfield name="city"/>
    Phone No:<s:textfield name="pno"/>
    Email:<s:textfield name="email"/>
    <s:submit value="Register"></s:submit>
</s:form>

Step 2: struts.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation  
//DTD Struts Configuration 2.1//EN"   
"http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<package name="default" extends="struts-default">  
<action name="reg" class="Register">  
<result name=""></result>  
</action>  </package> </struts>   

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <welcome-file-list>
   <welcome-file>home.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

Step 3: Register.java

public class Register
{
private String name,pwd,city,email;
private int pno;
//create getter and setter here
public void execute()
{
Login.send(this);
}}

Step 4: Login.java

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Login
{ public static void send(Register obj)
 {try{
Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession();
Transaction t=session.beginTransaction(); 
//Get and set  the values 
String name=obj.getName();
obj.setName(name);
String pwd=obj.getPwd();
obj.setPwd(pwd);
String city=obj.getCity();
obj.setCity(city);
int pno=obj.getPno();
obj.setPno(pno);
String email=obj.getEmail();
obj.setEmail(email);
session.save(obj);
t.commit();
session.close();
System.out.println("successfully saved");  
}catch(Exception e){System.out.println(e);}}}


Step 5: Create hibernate.cfg.xml & tablename.hbm.xml

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="Register.hbm.xml"/>
    </session-factory></hibernate-configuration>

Register.hbm.xml

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Register" table="REGISTER"> <id name="pno" type="int"> <column name="PNO" /> <generator class="assigned" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="pwd" type="java.lang.String"> <column name="PWD" /> </property> <property name="city" type="java.lang.String"> <column name="CITY" /> </property> <property name="email" type="java.lang.String"> <column name="EMAIL" /> </property> </class></hibernate-mapping>



Required Jar Files









No comments:

Post a Comment