Struts 2 with Hibernate

Struts 2 with Hibernate

Posted in : Java Posted on : October 1, 2011 at 6:00 PM Comments : [ 0 ]

In this section, you will learn how to integrate Struts 2 with Hibernate.

Struts 2 with Hibernate

In this section, you will learn how to integrate Struts 2 with Hibernate.

In the below example, we will add a record in the database using Hibernate. On addition of a record, all the data stored in the database will show up on the same page(in which you will enter page). You can delete any added record from the same page. The hyperlink is also provided on the same page which redirect us to the home page of the added person in the record.

The hierarchical structure of the project is given below :

The jar files used in the application is given below :


First, the following form will appear, in which you need to enter the details of the person whose record you want to add :

When you enter the value, it will add at the bottom table as given below :

As you can see, the table displayed below contains hyperlink of the person's homepage. Here you can delete the record of the person by clicking on the delete hyperlink.

Package Description

com.devmanuals.controller This package contains the java class which act as controller and will get the data from the database table and pass it to the view for presentation.
com.devmanuals.model This class contains the model class.
com.devmanuals.util This package contains the Util file that we use to create connection with hibernate.
com.devmanuals.view This package contains action class.



<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
<display-name>Contact Manager</display-name>




<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
body, input{
font-family: Calibri, Arial;
table#contact {
border-collapse: collapse;
th {
height: 40px;
background-color: #AAB6B3;
<title>Add New Record</title>

<h1>Struts2 Hibernate Example</h1>
<h3>Add New Record</h3>

<s:form action="add.action" method="post">
<s:textfield name="contact.firstName" label="Firstname"/>
<s:textfield name="contact.lastName" label="Lastname"/>
<s:textfield name="contact.emailId" label="Email"/>
<s:textfield name="contact.cellNo" label="Cell No."/>
<s:textfield name="" label="Homepage"/>
<s:textfield name="contact.birthDate" label="Birthdate"/>
<s:submit value="Add Contact" align="center"/>


<table id="contact" border="1">
<th>Cell No.</th>
<s:iterator value="contactList" >
<td><s:property value="lastName"/>, <s:property value="firstName"/> </td>
<td><s:property value="emailId"/></td>
<td><s:property value="cellNo"/></td>
<td><s:property value="birthDate"/></td>
<td><a href="<s:property value="website"/>">link</a></td>
<td><a href="delete.action?id=<s:property value="id"/>">delete</a></td>


<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

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

<package name="default" extends="struts-default" namespace="/">

<action name="add"
class="com.devmanuals.view.ContactAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>

<action name="delete"
class="com.devmanuals.view.ContactAction" method="delete">
<result name="success" type="chain">index.jsp</result>

<action name="index"
<result name="success">index.jsp</result>
<result name="input">index.jsp</result>
<action name="index.jsp"
<result name="success">index.jsp</result>
<result name="input">index.jsp</result>


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"



<!-- Database connection settings -->
<property name="connection.driver_class">
<property name="connection.url">
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="">update</property>

<mapping class="com.devmanuals.model.Contact" />



import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.devmanuals.model.Contact;
import com.devmanuals.util.HibernateUtil;

public class ContactManager extends HibernateUtil {

public Contact add(Contact contact) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
return contact;

public Contact delete(Long id) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Contact contact = (Contact) session.load(Contact.class, id);
if (null != contact) {
return contact;

public List<Contact> list() {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
List<Contact> contacts = null;
try {

contacts = (List<Contact>) session.createQuery("from Contact")

} catch (HibernateException e) {
return contacts;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

public class Contact implements Serializable{

private static final long serialVersionUID = -8767337896773261247L;

private Long id;
private String firstName;
private String lastName;
private String emailId;
private String cellNo;
private Date birthDate;
private String website;

private Date created;

public Long getId() {
return id;
public String getFirstName() {
return firstName;
public String getLastName() {
return lastName;
public String getEmailId() {
return emailId;
public String getCellNo() {
return cellNo;
public Date getBirthDate() {
return birthDate;
public String getWebsite() {
return website;
public Date getCreated() {
return created;
public void setId(Long id) { = id;
public void setFirstName(String firstName) {
this.firstName = firstName;
public void setLastName(String lastName) {
this.lastName = lastName;
public void setEmailId(String emailId) {
this.emailId = emailId;
public void setCellNo(String cellNo) {
this.cellNo = cellNo;
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
public void setCreated(Date created) {
this.created = created;
public void setWebsite(String website) { = website;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);

public static SessionFactory getSessionFactory() {
return sessionFactory;

import java.util.List;
import com.devmanuals.controller.ContactManager;
import com.devmanuals.model.Contact;

import com.opensymphony.xwork2.ActionSupport;

public class ContactAction extends ActionSupport {

private static final long serialVersionUID = 9149826260758390091L;
private Contact contact;
private List<Contact> contactList;
private Long id;

private ContactManager linkController;

public ContactAction() {
linkController = new ContactManager();
public String execute() {
//if(null != contact) {
this.contactList = linkController.list();
return SUCCESS;
public String add() {
try {
}catch(Exception e) {
return SUCCESS;
public String delete() {
return SUCCESS;
public Contact getContact() {
return contact;
public List<Contact> getContactList() {
return contactList;
public void setContact(Contact contact) { = contact;
public void setContactList(List<Contact> contactsList) {
this.contactList = contactsList;
public Long getId() {
return id;
public void setId(Long id) { = id;

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics