Struts - SPLessons

Struts 2 Hibernate

Chapter 25

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Hibernate

Struts 2 Hibernate

shape Description

Struts 2 Hibernate is like a database and it was developed by the Gavin king in 2001, the current version of hibernate is 4.3.6 was released in July 16th, 2014 and the main asset of hibernate is an ORM (Object Relational Model) and to vanish the flaws of JDBC hibernate came. ORM inside utilization Java Data Base Connectivity to furnish joining with the database.

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.

shape Example

In this example, let’s use the Struts 2 Hibernate integration.

shape Step 1

Create the project directory structure.

shape Step 2

Add the jar files in lib folder.

shape Step 3

Create the View pages.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@ taglib uri="/struts-tags" prefix="s" %>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <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>

Here just created the three text fields to enter the data such as Username, Password, Email and also created Login button.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">

<%@ taglib prefix="s" uri="/struts-tags" %>
     <title>Successful Login</title>
     <font color="green">
     Welcome to SPLessons
    <s:property value="name"/> Your login details successfully saved.

To display the success page if the page will execute well.

shape Step 4

Create the java files.

package com.splessons;
public class UserDetails
   private int id;
   private String username, password, email;
   public int getId()
     return id;
   public void setId(int 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)
    { = email; 
   public String execute()
          return "success";

Here created the data members and performed SET and GET methods on those data members.

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").
        Transaction t=session.beginTransaction(); 
        int i=(Integer); 
         return i; 

shape Step 5

Create the XML files.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "">
   <class name="com.splessons.UserDetails" table="LoginUserDetails" >
      <id name="id"> 
          <generator class="increment"></generator> 
     <property name="username"/> 
     <property name="password"/> 
     <property name="email"/>

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.

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" ""> 
              //Database connection properties
      <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
     <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe
    <property name="connection.username">system</property> 
    <property name="connection.password">system</property> 
               //hibernate properties
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="">create</property>
    <property name="show_sql">true</property>
             //mapping file properties
    <mapping resource="UserDetails.hbm.xml"/> 
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.

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.

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation //DTD Struts Configuration 2.1//EN" ""> 
   <package name="default" extends="struts-default"> 
      <action name="Login" class="com.splessons.UserDetails"> 
         <result name="success">success.jsp</result> 


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">

   <display-name>Struts 2</display-name>

shape Output

Run the application in the server, then the browser display the output as shown in the below image.

Enter all the required details in browser and click on Login button.

Check the output in database, for whether the details are successfully saved or not.

select * from LoginUserDetails;


shape Key Points

  • Struts 2 Hibernate – ORM is the one of the hibernate tool to fetch the data between relational databases.
  • Database connector jar file should be added to the project.
  • hibernate.cfg.xml file will have all the database properties.