To deal with hibernate project developer need to have database support like MySQL to create the table, the primary advantage of Hibernate code is creation of a table. Hibernate is also an open source and lightweight framework and It can be also known as ORM tool. Hibernate underpins inheritance also and underpins relationships like one to one, one to many and underpins collections also. ORM tool used to access the data. Struts 2 Hibernate is persistence.To overcome the flaws of JDBC hibernate came in to action.following are the flaws of JDBC.
Struts 2 application is supported with Database integration. One need to connect the Database in their application before, and then need to connect the JDBC (java naming directory) and Hibernate.
Step 2
index.jsp Here just created the three text fields to enter the data such as Username, Password, Email and also created Login button. To display the success page if the page will execute well. Step 3
<%@ 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" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<s:form action="Login" namespace="/">
<s:textfield name="username" label="Username"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:textfield name="email" label="Email"></s:textfield>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
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" %>
<html>
<head>
<title>Successful Login</title>
</head>
<body>
<font color="green">
<h1>
Welcome to SPLessons
</h1>
</font>
<h2>
<s:property value="name"/> Your login details successfully saved.
</h2>
</body>
</html>
USerDetails.java Here created the data members and performed SET and GET methods on those data members. Step 4
package com.splessons;
public class UserDetails
{
private int id;
private String username, password, email;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String execute()
{
UserDetailsDao.saveUserDetails(this);
return "success";
}
}
UserDetailsDao.java
package com.splessons;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UserDetailsDao
{
public static int saveUserDetails(UserDetails userDetails)
{
Session session=new Configuration().configure("hibernate.cfg.xml").
buildSessionFactory().openSession();
Transaction t=session.beginTransaction();
int i=(Integer)session.save(userDetails);
t.commit();
session.close();
return i;
}
}
The subelement of id used to generate the unique identifier for the objects of persistent class, increment generates the unique id only if no other process is inserting data into this table. It generates short, int or long type identifier. The configuration file will consist of connection properties, hibernate properties, mapping file name will consist of all the related class names. Dialect classes are used to convert HQL statements to database specific statements. To connect any database with hibernate, We need to specify the database Dialect class in hibernate. cfg. xml. If a show_sql value is true, generated the SQL statements in a console. web.xml Step 5
UserDetails.hbm.xml
<?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">
<hibernate-mapping>
<class name="com.splessons.UserDetails" table="LoginUserDetails" >
<id name="id">
<generator class="increment"></generator>
</id>
<property name="username"/>
<property name="password"/>
<property name="email"/>
</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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
//Database connection properties
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe
</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
//hibernate properties
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
//mapping file properties
<mapping resource="UserDetails.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Properties
Description
hibernate.connection.driver_class
The JDBC driver class.
hibernate.dialect
This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url
The JDBC URL to the database instance.
hibernate.connection.username
The database username.
hibernate.connection.password
The database password.
hibernate.connection.pool_size
Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit
Allows autocommit mode to be used for the JDBC connection.
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="Login" class="com.splessons.UserDetails">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>
/*
</url-pattern>
</filter-mapping>
</web-app>