Struts 1 Hibernate integration example

In this tutorial, we will see how to integrate Hibernate in a Struts 1 web application to perform some database operations.

So, We will :

  • Create a MySQL table : User
  • Create Eclipse web project and add the required libraries and tlds.
  • Add Struts 1 part : action, actionForm, JSP pages and Struts config file.
  • Add Hibernate part : model class, mapping file, Hibernate config file, Hibernate session provider and a DAO class.
  • Add the integration part : register a custom ServletContextListener to initialize Hibernate at application startup.


1. Technologies used

  • Struts 1.3.10
  • Hibernate 3.6.10
  • Eclipse 4.2
  • JDK 1.6
  • Tomcat 6.0


2. Create MySQL table : User


3. Project structure

We use Eclipse to create a new Dynamic Web Project.



4. Project libraries

We need to add required libraries to WEB-INF/lib folder :



5. Struts tlds

They will be used in the JSP pages.

We retrieve them from Struts-taglib-1.3.10  library.



6. Struts Action

It contains the business logic of the application.

When the Controller (ActionServlet) receives requests from the client, it uses struts-config.xml to select the appropriate Action for each request, and call the appropriate method of that action.


7. Struts ActionForm

It is used to map the data entered in the form fields with the corresponding attributes in this bean.


8. JSP pages

index.jsp : home page.


add.jsp : provides a form to add a new user.

The form tag associates all of the fields within this form with the UserActionForm.

search.jsp : used to search users by criteria.

In both JSP pages, we used the action path /user to process the form.


9. Struts configuration file

File : struts-config.xml

We defined an action mapping for UserAction and we added the parameter attribute whose value will be used by the action to decide what method to call.

To use this parameter, we declared a hidden field in our form with the same name (dispatch), so, when the form is submitted , the parameter value will be fetched by UserAction to call corresponding method. For example, if parameter value is addUser, the method to call will be addUser(...) .


10. Hibernate model class

File : /src/com/keylesson/persistence/


11. Hibernate mapping file

File : /src/com/keylesson/persistence/User.hbm.xml


12. Hibernate configuration file

File : hibernate.cfg.xml


13. Hibernate Utility class

It is used to build and store the Hibernate sessionFactory that will be used to retrieve the session instances.

File : HibernateUtil


14. Hibernate custom ServletContextListener

It is used to initialize the Hibernate sessionFactory by calling HibernateUtil class.

We will register it in the web.xml file.

File : HibernateServlet


15. DAO class

It provides two operations : create a new user and get list of users by criteria.

File : UserDAO


16. Web app configuration

We add the taglibs used in JSP pages, configure Struts ActionServlet instance and define a listener tag to start the Hibernate at application  startup.

File : web.xml


17. Test It

URL : http://localhost:8080/Struts1-Hibernate-XML/


We go to add user form :


We enter some values and click the submit button :


If we want to search user(s) :


We type (or not) some data and click the search button :



Download source code