Struts2 and Hibernate both are widely used frameworks in their respective area. Today we will learn how to integrate Struts2 Web application framework with Hibernate ORM framework.

Struts2 Hibernate

Unlike Spring Hibernate Integration, there are no plugins provided by Struts2 framework that we can use. So we would need to use the Servlet Listener to manage the Hibernate SessionFactory.

Let’s see how to integrate Struts2 with Hibernate with a simple web application example.

Our final project structure will look like below image, we will look into each of the component one by one. Start with creating a “Dynamic Web Project” in Eclipse and then convert it to maven project to get the basic skeleton ready for our web application.

Struts2-Hibernate-Example-450x374

Struts2 Hibernate Example Database Setup

We will create a login application, after authentication user will be present to the welcome page where all his information will be displayed. Below SQL script will create the required table and insert data that we will use for authentication.

setup.sql

Struts2, Hibernate and MySQL Maven Dependencies

We need to add Struts2, Hibernate and MySQL maven dependencies in pom.xml file, final pom.xml file looks like below.

We are using Hibernate 4 for our example, however same code will work with Hibernate 3 too. Although you might need to do some configuration hibernate 3 specific changes.

Struts2 Hibernate Example Deployment Descriptor

We need to plugin Struts2 framework in our web application by applying StrutsPrepareAndExecuteFilter in deployment descriptor.

web.xml

Notice that HibernateServletContextListener will be used to manage Hibernate SessionFactory, we will look it’s code later on.

Entity Bean

Our entity bean will also work as the struts2 action class model bean, it looks like below.

User.java

I am using XML based mappings, but we can also use JPA annotations based mapping for our entity bean.

Hibernate DAO Implementation Classes

Our UserDAO interface declares the method that will be exposed for User entity bean.

UserDAO.java

Hibernate specific DAO implementation looks like below.

UserDAOImpl.java

Servlet Context Listener

We will initialize Hibernate SessionFactory in the ServletContextListener implementation, when application will be stopped, we will destroy the SessionFactory.

HibernateServletContextListener.java

Struts2 Action Class

Our hibernate specific setup is ready, let’s move on to Struts2 action class.

LoginAction.java

Struts2, Hibernate Configuration and Hibernate Mapping XML

We have following URI configurations in Struts2 configuration file.

struts.xml

Hibernate Configuration File looks like below.

hibernate.cfg.xml

Hibernate Mapping XML file looks like below.

User.hbm.xml

Struts2 View Pages

As you can see from struts2 configuration file, we have three view pages. They are simple pages and looks like below.

login.jsp

welcome.jsp

error.jsp

Struts2 Hibernate Application Test

Our application is ready, build and deploy it into your favorite servlet container and you should get response pages like below images.

Struts2 Hibernate Login Page

Struts2-Hibernate-Login-Page

Struts2 Hibernate Welcome Page

Struts2-Hibernate-Login-Page

Struts2 Hibernate Login Error Page

You will also notice hibernate queries and other logs in the server log file.

Struts2 Hibernate Integration Summary

This tutorial was aimed to provide details about Struts2 and Hibernate integration, so that you can get started. As you can see that Servlet Context Listener makes it very easy to integrate. You can download the final project from below link.

By admin

Leave a Reply

%d bloggers like this: