Struts 2, Spring 4, Hibernate 4 and Maven Integration

In this tutorial I will show you how to integrate Struts 2, Spring 4, Hibernate 4 and Maven. In the previous example Integrate Spring 3, Struts 2 and Hibernate 3 , I have shown how to integrate Struts 2, Spring 3 and Hibernate 3 but I have not used maven there.

For this tutorial we will create maven based web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Prerequisites
The following configurations are required in order to run the application
Eclipse Kepler
JDK 1.8
Tomcat 8
Have maven installed and configured
Struts 2, Spring 4, Hibernate 4 dependencies in pom.xml
Now we will see the below steps how to create a maven based project in Eclipse
Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-webapp
Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : struts-spring-hibernate
The created project looks like below

struts spring hibernate maven

Step 2. Modify the pom.xml file as shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.roytuts</groupId>
    <artifactId>struts-spring-hibernate</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>struts-spring-hibernate Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <java.version>1.8</java.version>
        <spring.version>4.1.4.RELEASE</spring.version>
        <struts2.version>2.3.16.2</struts2.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
        <mysqlconnector.version>5.1.34</mysqlconnector.version>
    </properties>
    <dependencies>
        <!-- Spring 4 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- struts 2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <!-- struts 2 & spring -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <!-- hibernate 4 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- database pool -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>
        <!-- mysql java connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysqlconnector.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>struts-spring-hibernate</finalName>
        <plugins>
            <!-- use jdk 1.8 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Change also the Compiler compliance level as 1.8 from Java -> Compiler.
Step 4. Now when the build process finished then modify the web.xml file with below source code

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <!-- load spring configuration -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-config.xml</param-value>
    </context-param>
    <!-- load spring context -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- struts 2 filter and URL mapping -->
    <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>

Step 5. Create spring-config.xml file under src/main/resources directory with the below source code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    <!-- support spring annotation -->
    <context:annotation-config />
    <!-- support annotation transaction -->
    <tx:annotation-driven />
    <!-- declare datasource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/cdcol" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
    <!--Hibernate session factory configuration -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- load hibernate configuration file -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <!-- where to find the ORM classes -->
        <property name="packagesToScan" value="com.roytuts.hibernate.model" />
    </bean>
    <!-- Transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- Spring and Struts beans -->
    <!-- action -->
    <bean id="userDetailsAction" class="com.roytuts.struts.action.UserDetailsAction" />
    <!-- service -->
    <bean id="userDetailsService" class="com.roytuts.spring.service.impl.UserDetailsServiceImpl" />
    <!-- dao -->
    <bean id="userDetailsDao" class="com.roytuts.spring.dao.impl.UserDetailsDaoImpl" />
</beans>

Step 6. Create hibernate.cfg.xml file under src/main/resources directory

<?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>
        <!-- MySQL database -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- show SQL in console -->
        <property name="hibernate.show_sql">true</property>
        <!-- readable format for SQL output in the console -->
        <property name="format_sql">true</property>
    </session-factory>
</hibernate-configuration>

Step 7. Create struts.xml file under src/main/resources directory

<?xml version="1.0" encoding="UTF-8"?>
<!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" namespace="/" extends="struts-default">
        <!-- get single user details -->
        <!-- below class userDetailsAction is mapped in spring-config.xml file -->
        <action name="user" class="userDetailsAction" method="getAUserDetails">
            <result name="success">/user.jsp</result>
        </action>
        <!-- get all users details -->
        <action name="userList" class="userDetailsAction" method="getAllUserDetails">
            <result name="success">/listUsers.jsp</result>
        </action>
    </package>
</struts>

Step 8. Create MySQL table user_details in database cdcol

USE `cdcol`;
/*Table structure for table `user_details` */
DROP TABLE IF EXISTS `user_details`;
CREATE TABLE `user_details` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `dob` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Step 9. Dump some data into the table

/*Data for the table `user_details` */
insert  into `user_details`(`id`,`first_name`,`last_name`,`email`,`dob`) values (7,'Soumitra','Roy','contact@roytuts.com','30-08-1986'),(8,'Souvik','Sanyal','souvik.sanyal@email.com','30-09-1991');

Step 10. Now create Entity bean class which will map Java object to database table user_details

package com.roytuts.hibernate.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_details")
public class UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "email")
    private String email;
    @Column(name = "dob")
    private String dob;
    public UserDetails() {
    }
    public UserDetails(int id, String firstName, String lastName, String email, String dob) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.dob = dob;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getDob() {
        return dob;
    }
    public void setDob(String dob) {
        this.dob = dob;
    }
}

Step 11. Create DAO interface for querying database table

package com.roytuts.spring.dao;
import java.util.List;
import com.roytuts.hibernate.model.UserDetails;
public interface UserDetailsDao {
    public UserDetails getuserDetails(int id);
    public List<UserDetails> getAllUserDetails();
}

Step 12. Create the corresponding DAO implementation class

package com.roytuts.spring.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.roytuts.hibernate.model.UserDetails;
import com.roytuts.spring.dao.UserDetailsDao;
public class UserDetailsDaoImpl implements UserDetailsDao {
    @Autowired
    private SessionFactory sessionFactory;
    @Transactional
    @Override
    public UserDetails getuserDetails(int id) {
        UserDetails userDetails = (UserDetails) sessionFactory.getCurrentSession().get(UserDetails.class, id);
        return userDetails;
    }
    @Transactional
    @Override
    public List<UserDetails> getAllUserDetails() {
        @SuppressWarnings("unchecked")
        List<UserDetails> userDetails = (List<UserDetails>) sessionFactory.getCurrentSession()
                .createCriteria(UserDetails.class).list();
        //or below query
        /*
         * @SuppressWarnings("unchecked") List<UserDetails> userDetails =
         * (List<UserDetails>)sessionFactory.getCurrentSession().createQuery(
         * "from UserDetails").list();
         */
        return userDetails;
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Step 13. Create the service interface for processing logic or business logic

package com.roytuts.spring.service;
import java.util.List;
import com.roytuts.hibernate.model.UserDetails;
public interface UserDetailsService {
    public UserDetails getuserDetails(int id);
    public List<UserDetails> getAllUserDetails();
}

Step 14. Create the corresponding service implementation class

package com.roytuts.spring.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.roytuts.hibernate.model.UserDetails;
import com.roytuts.spring.dao.UserDetailsDao;
import com.roytuts.spring.service.UserDetailsService;
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserDetailsDao userDetailsDao;
    @Override
    public UserDetails getuserDetails(int id) {
        return userDetailsDao.getuserDetails(id);
    }
    @Override
    public List<UserDetails> getAllUserDetails() {
        return userDetailsDao.getAllUserDetails();
    }
    public UserDetailsDao getUserDetailsDao() {
        return userDetailsDao;
    }
}

Step 15. Create struts 2 action class which will handle user request and response

package com.roytuts.struts.action;
import java.util.List;
import org.apache.struts2.dispatcher.DefaultActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import com.roytuts.hibernate.model.UserDetails;
import com.roytuts.spring.service.UserDetailsService;
public class UserDetailsAction extends DefaultActionSupport {
    private static final long serialVersionUID = 1L;
    @Autowired
    private UserDetailsService userDetailsService;
    private UserDetails userDetails;
    private List<UserDetails> userDetailsList;
    public String getAUserDetails() {
        if (userDetails == null) {
            int id = 7;// should come from UI
            userDetails = userDetailsService.getuserDetails(id);
        }
        return SUCCESS;
    }
    public String getAllUserDetails() {
        if (userDetailsList == null) {
            userDetailsList = userDetailsService.getAllUserDetails();
        }
        return SUCCESS;
    }
    public UserDetails getUserDetails() {
        return userDetails;
    }
    public void setUserDetails(UserDetails userDetails) {
        this.userDetails = userDetails;
    }
    public List<UserDetails> getUserDetailsList() {
        return userDetailsList;
    }
    public void setUserDetailsList(List<UserDetails> userDetailsList) {
        this.userDetailsList = userDetailsList;
    }
    public UserDetailsService getUserDetailsService() {
        return userDetailsService;
    }
}

Step 16. Now create user.jsp file for displaying single user details

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2, Spring 4 and Hibernate 4 Integration Example</title>
<s:if test="userDetails != null">
    <h2>User Details</h2>
    <table cellpadding="5px" border="1px">
        <tbody>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>DOB</th>
            </tr>
            <tr>
                <td><s:property value="userDetails.id"></s:property></td>
                <td><s:property value="userDetails.firstName"></s:property></td>
                <td><s:property value="userDetails.lastName"></s:property></td>
                <td><s:property value="userDetails.email"></s:property></td>
                <td><s:property value="userDetails.dob"></s:property></td>
            </tr>
        </tbody>
    </table>
</s:if>
</body>
</html>

In the above file we have used struts 2 taglib for accessing the bean property.

Step 17. Create listusers.jsp file for displaying all users details

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2, Spring 4 and Hibernate 4 Integration Example</title>
<s:if test="userDetailsList.size() > 0">
    <h2>List of User Details</h2>
    <table cellpadding="5px" border="1px">
        <tbody>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>DOB</th>
            </tr>
            <s:iterator value="userDetailsList">
                <tr>
                    <td><s:property value="id"></s:property></td>
                    <td><s:property value="firstName"></s:property></td>
                    <td><s:property value="lastName"></s:property></td>
                    <td><s:property value="email"></s:property></td>
                    <td><s:property value="dob"></s:property></td>
                </tr>
            </s:iterator>
        </tbody>
    </table>
</s:if>
</body>
</html>

Step 18. Now run the application on Tomcat server 8 and when the application successfully deployed onto the server, please hit the URL http://localhost:8080/struts-spring-hibernate/userList , you will below output in the browser

struts spring hibernate maven

The corresponding console output

Hibernate: select userdetail0_.id as id1_0_0_, userdetail0_.dob as dob2_0_0_, userdetail0_.email as email3_0_0_, userdetail0_.first_name as first_na4_0_0_, userdetail0_.last_name as last_nam5_0_0_ from user_details userdetail0_ where userdetail0_.id=?

When you hit the URL http://localhost:8080/struts-spring-hibernate/user in the browser, you will see the below output

struts spring hibernate maven

The corresponding console output

Hibernate: select this_.id as id1_0_0_, this_.dob as dob2_0_0_, this_.email as email3_0_0_, this_.first_name as first_na4_0_0_, this_.last_name as last_nam5_0_0_ from user_details this_

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *