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.  




index.jsp

<html>

<head>

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

<title>Home Page</title>

</head>

<body>

<h2>Loding....</h2>

</body>

</html>

customerform.jsp

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

<html>

<head>

<title>Customer Information Form</title>

<s:head />

</head>

<body>

<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" />

</s:form>

</body>

</html>

HibernateStrutsInsert.jsp

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;

@Override

public String execute() throws Exception {

return SUCCESS;

}

public String dataInsert() {

obInfoDAO.saveInfo(obModel);

System.out.println("inserted");

return SUCCESS;

}

@Override

public CustomerModel getModel() {

// TODO Auto-generated method stub

obModel = new CustomerModel();

return obModel;

}

}

customerfoDAO.java

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()

.configure().buildSessionFactory();

Session session = sessionFactory.openSession();

session.beginTransaction();

session.save(obCustomerModel);

session.getTransaction().commit();

return obCustomerModel;

 }

}

struts.xml

<?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>

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

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

<action name="hello" >

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

</action>

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

method="dataInsert">

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

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

</action>

</package>

</struts>

CustomerModel.java 

package net.roseindia.model;

import java.io.Serializable;

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) 

{this.name = name;}

public void setAddress(String address) {

this.address = address;}

public void setContact(String contact) {

this.contact = contact;}

public void setEmail(String email) {

this.email = email;}}

CustomerModel.hbm.xml 

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"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

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

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

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

</id>

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

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

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

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

</class>

</hibernate-mapping>

success.jsp

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

<html>

<head>

<title>View Information</title>

</head>

<body>

<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/>

</body>

</html>

 

Download Source Code


Related Tags for struts hibernate integration application:

Advertisements

Ads

Ads

 
Advertisement null

Ads