Hibernate

Struts And Hibernate Connectivity
Previous Home Next

There is a code of Struts and hibernate connectivity ,Steps required are as follows: This example show how to create login form using struts and hibernate.

Creating

hibernate.cfg.xml file

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

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe
</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<property name="connection.driver_class"
>oracle.jdbc.driver.OracleDriver</property>
<mapping resource="Login.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Login.hbm.xml file

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

<!-- r4r.co.in. -->
<hibernate-mapping>
<class name="mypack.Login">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="password"/>
</class>
</hibernate-mapping>

Login.java

package mypack;

public class Login {
int id;
String name,password;
public Login() {
super();
}
public Login(String name, String password) {
super();
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:textfield name="password" label="Password"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>

a.jsp

successfully login

b.jsp

incorrect username or password

DAO class

package mypack;

import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class DAO {
@SuppressWarnings("unchecked")
public boolean find(String name,String password)
{
Configuration cfg=new Configuration().configure();
SessionFactory f=cfg.buildSessionFactory();
Session session=f.openSession();
//Scanner in=new Scanner(System.in);
Query q=session.createQuery(
"from Login e where e.name=? and e.password=?");
q.setParameter(0,name);
q.setParameter(1,password);
Iterator<Login> itr=q.iterate();
while(itr.hasNext())
{
session.close();
return true;
}
session.close();
return false;
}
}

Struts.xml file


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//
DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="login" class="mypack.LoginAction">
<result name="success">/a.jsp</result>
<result name="failure">/b.jsp</result>
</action>
</package>
</struts> 

LoginAction.java class

package mypack;

public class LoginAction {

Login l;
String name;
String password;

public String execute()
{
DAO dao=new DAO();
if(dao.find(getName(), getPassword()))
return "success";
else
return "failure";
}
public Login getL() {
return l;
}
public void setL(Login l) {
this.l = l;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: