Share on Google+Share on Google+

Hibernate Struts Integration

In this section, you will learn Hibernate Struts Integration.

Hibernate Struts Integration

In this section, you will learn Hibernate Struts Integration.

Apache struts is the effective, efficient and extensible MVC based open source framework for developing Java web application. It supports the full development life cycle from developing, deploying to maintenance.

The WebWork and  Struts communities jointly work together to develop Struts2.


In the below example, we will create a login application using Struts and Hibernate.

The project hierarchy and the jar file used is given below :

The SQL query used to create table login is below :

CREATE TABLE `login` ( 
`loginid` varchar(30) NOT NULL, 
`password` varchar(30) default NULL, 
PRIMARY KEY (`loginid`) 



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


<?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" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/">
<action name="login" class="net.roseindia.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="error">Login.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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="">none</property>

<mapping class="net.roseindia.Login"/>


label.username= UserName
label.password= Password
label.login= Login
error.login= UserName / Password Not Matching. Try again...

package net.roseindia;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
Configuration cfg = new Configuration();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
return cfg.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);

public static SessionFactory getSessionFactory() {
return sessionFactory;

package net.roseindia;


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

public class Login implements Serializable{
private String loginid;

private String password;

public String getLoginid() {
return loginid;
public void setLoginid(String loginid) {
this.loginid = loginid;

public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;

package net.roseindia;

import org.hibernate.Query;
import org.hibernate.Session;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String username;
private String password;

Session session=HibernateUtil.getSessionFactory().openSession();

public String execute() {
Query forlogin = session.createQuery("from Login as l where l.loginid = '"+this.username+"' and l.password='"+this.password+"'");
if (forlogin.list().size()==1) {
return "success";
} else {
return "error";
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title> World Struts Tutorial</title>

<h2>Struts 2 Login Example</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<h3>HELLO !! <font color=blue><s:property value="username" /> </font>!</h3>
<h2><font color=red>Welcome To RoseIndia</font></h2>


After execution, you will get the following page on your web browser :

The username is deepak and password is deepak . When you login correctly, you will get the following page :


If login is incorrect, you will get the following page :


Download  Source Code



Posted on: March 27, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Struts Integration  

Post your Comment

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