Hibernate and Struts 2 Integration

We can incorporate any struts application with hibernate. There is no prerequisite of additional endeavors.

In this precedent, we going to utilize struts 2 structure with hibernate. You need container records for struts 2 and hibernate.

Example of Hibernate and struts2 integration

In this precedent, we are making the enlistment structure utilizing struts2 and putting away this information into the database utilizing Hibernate. How about we see the documents that we ought to make to coordinate the struts2 application with hibernate.

  • index.jsp record to get contribution from the user.
  • User.java An activity class for dealing with the solicitation. It utilizes the dao class to store the data.
  • RegisterDao.java A java class that utilizes DAO configuration example to store the information utilizing hibernate.
  • user.hbm.xml A mapping document that contains data about the steady class. For this situation, activity class fills in as the constant class.
  • hibernate.cfg.xml An arrangement record that contains informations about the database and mapping file.
  • struts.xml document contains data about the activity class and result page to be invoked.
  • welcome.jsp A jsp document that shows the appreciated data with username.
  • web.xml A web.xml document that contains data about the Controller of Struts framework.


In this page, we have made a structure utilizing the struts labels. The activity name for this structure is register.

<%@ 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 a simple POJO class. Here it works as the action class for struts and persistent class for hibernate. It calls the register method of RegisterDao class and returns success as the string.

    1. package com.javacodegeeks;  
    3. public class User {  
    4. private int id;  
    5. private String name;  
    6. //getters and setters  
    8. public String execute(){  
    9.     RegisterDao.saveUser(this);  
    10.     return "success";  
    11. }  
    13. }  


    It is a java class that saves the object of User class using the Hibernate framework.

    1. package com.javacodegeeks;  
    3. import org.hibernate.Session;  
    4. import org.hibernate.Transaction;  
    5. import org.hibernate.cfg.Configuration;  
    7. public class RegisterDao {  
    9. public static int saveUser(User u){  
    11. Session session=new Configuration().  
    12. configure("hibernate.cfg.xml").buildSessionFactory().openSession();  
    14. Transaction t=session.beginTransaction();  
    15. int i=(Integer)session.save(u);  
    16. t.commit();  
    17. session.close();  
    19. return i;  
    21. }  
    23. }  


    This mapping file contains all the information of the persitent class.

    1. <?xml version='1.0' encoding='UTF-8'?>  
    2. <!DOCTYPE hibernate-mapping PUBLIC  
    3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
    6. <hibernate-mapping>  
    7. <class name="com.javacodegeeks.User" table="user451">  
    8. <id name="id">  
    9. <generator class="increment"></generator>  
    10. </id>  
    11. <property name="name"></property>  
    12. </class>  
    14. </hibernate-mapping>  


    This configuration file contains informations about the database and mapping file. Here, we are using the hb2ddl.auto property, so you don't need to create the table in the database.

    1. <?xml version='1.0' encoding='UTF-8'?>  
    2. <!DOCTYPE hibernate-configuration PUBLIC  
    3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
    6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
    7. <hibernate-configuration>  
    9. <session-factory>  
    10. <property name="hbm2ddl.auto">update</property>  
    11. <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
    12. <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
    13. <property name="connection.username">system</property>  
    14. <property name="connection.password">oracle</property>  
    15. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
    16. <mapping resource="user.hbm.xml"/>  
    18. </session-factory>  
    20. </hibernate-configuration>  


    This files contains information about the action class to be invoked. Here the action class is User.

    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation  
    3. //DTD Struts Configuration 2.1//EN"   
    4. "http://struts.apache.org/dtds/struts-2.1.dtd">  
    5. <struts>  
    7. <package name="abc" extends="struts-default">  
    8. <action name="register" class="com.javacodegeeks.User">  
    9. <result name="success">welcome.jsp</result>  
    10. </action>  
    11. </package>  
    12. </struts>      


    It is the welcome file, that displays the welcome message with username.

    1. <%@ taglib uri="/struts-tags" prefix="S" %>  
    3. Welcome: <S:property value="name"/>  


    It is web.xml file that contains the information about the controller. In case of Struts2, StrutsPrepareAndExecuteFilter class works as the controller.

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    7.   <welcome-file-list>  
    8.     <welcome-file>index.jsp</welcome-file>  
    9.   </welcome-file-list>  
    10.   <filter>  
    11.     <filter-name>struts2</filter-name>  
    12.     <filter-class>  
    13.   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
    14.     </filter-class>  
    15.   </filter>  
    16.   <filter-mapping>  
    17.     <filter-name>struts2</filter-name>  
    18.     <url-pattern>/*</url-pattern>  
    19.   </filter-mapping>  
    20. </web-app>   <?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>

    javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition