Introduction📄
Hibernate is a high-performance object/relational persistence and query service that is open source and available for free download under the GNU Lesser General Public Licence (LGPL).
This article will teach us how to achieve Struts 2 integration with Hibernate.
⭐Examples Of Hibernate and Struts Integration⭐
In this example, we're using struts2 to build the registration form and hibernate to save the data in the database. We must build a few files to integrate the struts2 application with hibernate.
index.jsp: To collect user input.
User.java: A class of actions for dealing with the request. The data is stored using the dao class.
RegisterDao.java: A Java class that stores the data using the DAO design paradigm and hibernates.
user.hbm.xml: An informational mapping file for the persistent class. The action class serves as the permanent class in this scenario.
hibernate.cfg.xml: A configuration file that includes details about the mapping file and database.
struts.xml: Information about the action class and result page to be invoked is contained in this file.
welcome.jsp: A jsp file that shows the login and welcome text.
web.xml: A web.xml file that is part of the Struts framework and contains details about the Controller.
Now, let’s see each of these files in detail:
⭐Index.jsp
Using the struts tags, we have built a form on this website. The register is the name of the action for this form.
<%@ taglib uri="/struts-tags" prefix="S" %>
<S:form action="register">
<S:textfield name="name" label="Name"></S:textfield>
<S:submit value="register"></S:submit>
</S:form>
⭐User.java
It is just a basic POJO class. Here, it serves as the persistent class for hibernating and the action class for struts. The RegisterDao class's register method is called, and success is returned as a string.
package com.javatpoint;
public class User {
private int id;
private String name;
//getters and setters
public String execute(){
RegisterDao.saveUser(this);
return "success";
}
}
⭐RegisterDao.java
It is a Java class that uses the Hibernate framework to save an object from the User class.
package com.javatpoint;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class RegisterDao {
public static int saveUser(User u){
Session session=new Configuration().
configure("hibernate.cfg.xml").buildSessionFactory().openSession();
Transaction t=session.beginTransaction();
int i=(Integer)session.save(u);
t.commit();
session.close();
return i;
}
}
⭐user.hbm.xml
The complete information for the persistent class is contained in this mapping file.
<?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.javatpoint.User" table="user451">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
⭐hibernate.cfg.xml
Information about the database and mapping file is contained in this configuration file. You don't need to build the table in the database because of the hb2ddl.auto property is being used in this case.
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
⭐struts.xml
Information about the action class that will be called is contained in this file. Here, the User is the action class.
<?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="abc" extends="struts-default">
<action name="register" class="com.javatpoint.User">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
⭐welcome.jsp
The welcome message and username are displayed in the welcome file.
<%@ taglib uri="/struts-tags" prefix="S" %>
Welcome: <S:property value="name"/>
⭐web.xml
The information about the controller is in the web.xml file. The controller for Struts2 is the StrutsPrepareAndExecuteFilter class.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.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>
Frequently Asked Questions
Can we use hibernate with Struts?
To set the Hibernate session factory in the servlet context, create a new Hibernate Struts plug-in file and include it in the struts-config.xml file. Get the Hibernate session factory from the servlet context in Struts and perform any Hibernate activity you desire.
What function do interceptors provide in Struts2?
It inserts error data from string to parameter type conversion into the action's errors field. If no HTTP session is present, one is automatically created. It gives the developer a variety of debugging windows.
What is the struts2 framework?
A beautiful, adaptable framework for building Java web applications that are business-ready is Apache Struts 2. This framework aims to speed up the entire application development process, from creation to deployment and ongoing maintenance. Web Work 2 was the previous name for Apache Struts 2.
What does Struts2 interceptor stack mean?
An interceptor stack is made using the interceptor-stack element. An assortment of interceptors is found in a stack. The interceptor-ref element is used to define each interceptor in the stack. In this illustration, we'll build a stack like the default stack and modify the validation interceptor to suit our needs.
Why do we use the struts framework?
Strut is an open-source framework that uses a Model, View, Controller (MVC) architecture and extends the Java Servlet API. It lets you develop online applications that are versatile, maintainable, and extensible using industry-standard technologies like XML, resource bundles, and JSP pages.
Conclusion
This article taught us about the struts 2 frameworks and their integration with hibernate. Lastly, we saw different files to build an application in struts 2 with the help of hibernating.
You can refer to the 19 best python frameworks and flask introduction to learn more about different frameworks.
That's the end of the article. I hope you all like this article.
Do upvote our blogs if you find them helpful and engaging!
Happy Learning, Ninjas!