Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Hibernate with Struts 2


We can integrate any struts application with hibernate. There is no requirement of extra efforts.

In this example, we going to use struts 2 framework with hibernate. You need to have jar files for struts 2 and hibernate.


 

Example of Hibernate and struts2 integration

In this example, we are creating the registration form using struts2 and storing this data into the database using Hibernate. Let’s see the files that we should create to integrate the struts2 application with hibernate.

  • index.jsp file to get input from the user.
  • User.java A action class for handling the request. It uses the dao class to store the data.
  • RegisterDao.java A java class that uses DAO design pattern to store the data using hibernate.
  • user.hbm.xml A mapping file that contains information about the persistent class. In this case, action class works as the persistent class.
  • hibernate.cfg.xml A configuration file that contains informations about the database and mapping file.
  • struts.xml file contains information about the action class and result page to be invoked.
  • welcome.jsp A jsp file that displays the welcome information with username.
  • web.xml A web.xml file that contains information about the Controller of Struts framework.

index.jsp

In this page, we have created a form using the struts tags. The action name for this form is register.

  1. <%@ taglib uri=“/struts-tags” prefix=“S” %>
  2. <S:form action=“register”>
  3. <S:textfield name=“name” label=“Name”></S:textfield>
  4. <S:submit value=“register”></S:submit>
  5. </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.Kreationnext;
  2. public class User {
  3. private int id;
  4. private String name;
  5. //getters and setters
  6. public String execute(){
  7.     RegisterDao.saveUser(this);
  8.     return “success”;
  9. }
  10. }

RegisterDao.java

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

  1. package com.Kreationnext;
  2. import org.hibernate.Session;
  3. import org.hibernate.Transaction;
  4. import org.hibernate.cfg.Configuration;
  5. public class RegisterDao {
  6. public static int saveUser(User u){
  7. Session session=new Configuration().
  8. configure(“hibernate.cfg.xml”).buildSessionFactory().openSession();
  9. Transaction t=session.beginTransaction();
  10. int i=(Integer)session.save(u);
  11. t.commit();
  12. session.close();
  13. return i;
  14. }
  15. }

user.hbm.xml

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”>
  5. <hibernate-mapping>
  6. <class name=“com.Kreationnext.User” table=“user451”>
  7. <id name=“id”>
  8. <generator class=“increment”></generator>
  9. </id>
  10. <property name=“name”></property>
  11. </class>
  12. </hibernate-mapping>

hibernate.cfg.xml

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”>
  5. <!– Generated by MyEclipse Hibernate Tools.                   –>
  6. <hibernate-configuration>
  7. <session-factory>
  8. <property name=“hbm2ddl.auto”>update</property>
  9. <property name=“dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  10. <property name=“connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  11. <property name=“connection.username”>system</property>
  12. <property name=“connection.password”>oracle</property>
  13. <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  14. <mapping resource=“user.hbm.xml”/>
  15. </session-factory>
  16. </hibernate-configuration>

struts.xml

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>
  6. <package name=“abc” extends=“struts-default”>
  7. <action name=“register” class=“com.Kreationnext.User”>
  8. <result name=“success”>welcome.jsp</result>
  9. </action>
  10. </package>
  11. </struts>

welcome.jsp

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

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

web.xml

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>

Comments

comments