Monday 11 July 2016

REGISTRATION FORM STRUTS 2 HIBERNATE USING ECLIPSE

The Following Software are used:
1.Create 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="Registration">
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></td>
 </s:form>
2.Create web.xml and struts.xml file

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>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- Configuration for the default package. -->
     <package name="default" extends="struts-default">
        <action name="Registration" class="Registration">
            <result name="SUCCESS">/success.jsp</result>
             <result name="ERROR">/Error.jsp</result>
        </action>
    </package>
</struts>

3. Create Action class and POJO class

Employee.java

public class Employee
{private int pno;
    private String name,email,pwd,city;
//Use Getter and Setter here}

Registration.java

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class Registration extends ActionSupport implements ModelDriven<Object>, Preparable {
private static final long serialVersionUID = 1L;
Employee employee;
SessionFactory sessionFactory = null;
Session session = null;
    public String execute() {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(employee);
            transaction .commit();
            System.out.println("Record Inserted Successfully");
        } 
        catch(HibernateException hibernateException)
        {
            System.out.println(hibernateException.getMessage());
            session.close();
            return "ERROR";}
        return "SUCCESS";
    }
    public Employee getModel() {
        return employee;
    }
    public void prepare() throws Exception {
        employee = new Employee();
    }}

4. Create hbm and cfg file


Employee.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">
<!-- Generated Jul 11, 2016 4:53:11 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="Employee" 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="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="pwd" type="java.lang.String">
            <column name="PWD" />
        </property>
        <property name="city" type="java.lang.String">
            <column name="CITY" />
        </property>
    </class></hibernate-mapping>

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="Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

5.Create Success and Error page

success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s"  uri="/struts-tags"%>
     <body><center>
     <br>  <br>  <br>  <br>
     <h1 style="color:chocolate"> <s:property value="name"/> registered successfully !!</h1>
      </center>

error.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="Error.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
     <body>
        <h1>Error Occurred !!  </h1>
    </body>

Required Jars


Add caption


      
   


Database Table Design    

    




1 comment:

  1. how login admin staff student in single page in struts2 hibernate????

    ReplyDelete