struts hibernate integration application
Posted on: March 29, 2011 at 12:00 AM
struts hibernate integration application

Struts2 hibernate integration application.

In this tutorial we are going to show how to Integrate Struts Hibernate and create an application. This struts hibernate integration application will use hbm.xml for mapping metadata. You can also use annotation as mapping metadata.

About struts hibernate integration application example

This example we are going to display create a form for data input. User will be able to enter following fields:

  • Customer Name
  • Address
  • Contact
  • Customer E-mail

Application performs the validation and if there is some error it is displayed on the form. This struts hibernate integration application is very basic example and you can extend the example to create your own application.

We have used MySQL database as backend of the struts hibernate integration application example.

Description: It is a struts2 and hibernate integration application. In this application,
 you will see how to integrate struts and hibernate and how to create mapping using hbm file.  




<meta http-equiv="refresh" content="1;URL='hello.action'">

<title>Home Page</title>







<%@taglib prefix="s" uri="/struts-tags"%>



<title>Customer Information Form</title>

<s:head />



<h2>Add Customer Information</h2>

<s:form action="addInfo">

<s:textfield key="name" label="Customer Name" />

<s:textfield key="address" label="Address" />

<s:textfield key="contact" label="Contact" />

<s:textfield key="email" label="Customer E-mail" />

<s:submit value="Submit" />





package net.roseindia.action;

import net.roseindia.dao.CustomerInfoDAO;

import net.roseindia.model.CustomerModel;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

public class HibernateStrutsInsert extends ActionSupport implements

ModelDriven<CustomerModel> {

CustomerInfoDAO obInfoDAO = new CustomerInfoDAO();

CustomerModel obModel;


public String execute() throws Exception {

return SUCCESS;


public String dataInsert() {



return SUCCESS;



public CustomerModel getModel() {

// TODO Auto-generated method stub

obModel = new CustomerModel();

return obModel;



package net.roseindia.dao;

import net.roseindia.model.CustomerModel;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

public class CustomerInfoDAO {

public CustomerModel saveInfo(CustomerModel obCustomerModel) {

SessionFactory sessionFactory = new AnnotationConfiguration()


Session session = sessionFactory.openSession();



return obCustomerModel;




<?xml version="1.0" encoding="UTF-8"?>


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"



<constant name="struts.devMode" value="false" />

<package name="roseindia" extends="struts-default">

<action name="hello" >

<result name="success">jsp/customerform.jsp</result>


<action name="addInfo" class="net.roseindia.action.HibernateStrutsInsert"


<result name="input">jsp/customerform.jsp</result>

<result name="success">jsp/success.jsp</result>




package net.roseindia.model;


public class CustomerModel implements Serializable {

private int custId;

private String name;

private String address;

private String contact;

private String email;

public int getCustId() { return custId; }

public String getName() { return name; }

public String getAddress() { return address; }

public String getContact() {return contact;}

public String getEmail() { return email; }

public void setCustId(int custId) 

{ this.custId = custId;}

public void setName(String name) 

{ = name;}

public void setAddress(String address) {

this.address = address;}

public void setContact(String contact) { = contact;}

public void setEmail(String email) { = email;}}


It is a xml file. In this file; we provides the mapping of 
all properties of model class to the column of database table.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"



<class name="net.roseindia.model.CustomerModel" table="custinfo">

<id name="custId" column="">

<generator class="increment"></generator>


<property name="name" column="cname"></property>

<property name="address" column="caddress"></property>

<property name="contact" column="contact"></property>

<property name="email" column="cemail"></property>




<%@taglib prefix="s" uri="/struts-tags"%>



<title>View Information</title>



<h2>Customer Information </h2>

Name : <s:property value="name" /><br/>

Address : <s:property value="address" /><br/>

Contact :<s:property value="contact" /><br/>

Email : <s:property value="email" /><br/>




Download Source Code

Related Tags for struts hibernate integration application:




Advertisement null