Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Struts Integration with Hibernate

Introduction

Struts is a popular framework for building web applications in Java, and Hibernate is a powerful ORM (Object-Relational Mapping) framework. Integrating Struts with Hibernate allows developers to manage database interactions more efficiently while following the MVC (Model-View-Controller) design pattern. This tutorial will walk you through the steps to integrate these two frameworks, providing examples and explanations along the way.

Prerequisites

Before we begin, ensure you have the following:

  • Java Development Kit (JDK) installed
  • Apache Struts library
  • Hibernate Core library
  • A servlet container (like Apache Tomcat)
  • An IDE (like Eclipse or IntelliJ IDEA)

Setting Up Your Project

Start by creating a new Dynamic Web Project in your IDE. Add the necessary libraries for Struts and Hibernate to your project’s build path. You can download the libraries from the respective official websites or use a dependency management tool like Maven or Gradle.

Adding Dependencies (Maven)

Here’s how to add Struts and Hibernate dependencies in your pom.xml:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.20</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.30.Final</version>
</dependency>
                    

Configuring Hibernate

Create a hibernate.cfg.xml file in the src/main/resources directory to configure Hibernate. This XML file contains database connection details and mappings.

Example Configuration

<?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>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/yourdbname</property>
        <property name="hibernate.connection.username">yourusername</property>
        <property name="hibernate.connection.password">yourpassword</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>
                    

Creating a Struts Action Class

Create an action class that will handle requests and interact with Hibernate. This class will be responsible for processing user input and returning a result.

Example Action Class

public class UserAction extends ActionSupport {
    private User user;
    
    public String execute() {
        // Logic to save user using Hibernate
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.save(user);
        tx.commit();
        session.close();
        return SUCCESS;
    }

    // Getters and Setters for user
}
                    

Configuring Struts

Create a struts.xml file in the src/main/resources directory to configure your Struts actions.

Example struts.xml

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" extends="struts-default">
        <action name="saveUser" class="com.example.UserAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>
                    

Creating the JSP View

Finally, create a JSP page to accept user input and display results. This page will use the Struts tag library to bind form data to the action class.

Example JSP Page

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>User Form</title>
</head>
<body>
    <h2>User Registration</h2>
    <s:form action="saveUser">
        <s:textfield name="user.name" label="Name" />
        <s:textfield name="user.email" label="Email" />
        <s:submit value="Submit" />
    </s:form>
</body>
</html>
                    

Running the Application

Deploy your application on the servlet container and access it through your web browser. You should see the user registration form. Fill in the details and submit the form to see how Struts interacts with Hibernate to save the data in the database.

Conclusion

In this tutorial, you learned how to integrate Struts with Hibernate. We covered setting up the project, configuring Hibernate, creating an action class, configuring Struts, and creating a JSP view. This integration allows you to effectively manage database operations while adhering to the MVC pattern in your web applications.