Hibernate Struts Integration

In this section, you will learn Hibernate Struts Integration.

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.

Example

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`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

CODE

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>HibernateStrutsIntegration</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>

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.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>
</action>
</package>
</struts>

hibernate.cfg.xml

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

<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.10.13:3306/anky</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="hbm2ddl.auto">none</property>

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

</session-factory>
</hibernate-configuration>

ApplicationResources.properties

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

HibernateUtil.java

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();
cfg.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
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;
}
}

Login.java

package net.roseindia;

import java.io.Serializable;

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

@Entity
@Table(name="login")
public class Login implements Serializable{
@Id
@Column(name="loginid")
private String loginid;

@Column(name="password")
private String password;

@Column(name="loginid")
public String getLoginid() {
return loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}

@Column(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

LoginAction.java

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 {
addActionError(getText("error.login"));
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;
}
}

Login.jsp

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

<body>
<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" />
</s:form>
</body>
</html>

Welcome.jsp

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

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

OUTPUT

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