Struts - SPLessons
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. index.jsp [java] <%@ 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> [/java] Here just created the three text fields to enter the data such as Username, Password, Email and also created Login button. success.jsp [java]<%@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> [/java] To display the success page if the page will execute well.

shape Step 4

Create the java files. USerDetails.java [java] 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"; } } [/java] Here created the data members and performed SET and GET methods on those data members. UserDetailsDao.java [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; } } [/java]

shape Step 5

Create the XML files. UserDetails.hbm.xml [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> [/xml] 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. hibernate.cfg.xml [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>   [/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. struts.xml [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] web.xml [xml] <?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> [/xml]

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. [sql]select * from LoginUserDetails;[/sql]

Summary

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.