Simple Procedure For Developing Struts application with Hibernate
Step 1: Develop a search.jsp file using Struts Tag Libraries.
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:form action="/us.do">
firstName:<html:text property="fname"/><br>
<html:submit value="Search"/>
</html:form>
Step2 : Develop SearchUserDTO.java file
package info.inetsolv.dto;public class SearchUserDTO{
String fname;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
}
Step3 : Develop UsersVO.java file
package info.inetsolv.vo;
public class UsersVO {
String fname;
String lname;
String email;
String pwd;
String dob;
String gender;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
step 4: Develop a SearchUserDAO.java file
package info.inetsolv.dao;import info.inetsolv.dto.*;
import info.inetsolv.util.*;
import java.util.*;
public class SearchUserDAO{
public ArrayList searchUsers(SearchUserDTO srdto) {
String hqlQuery = "from info.inetsolv.vo.UsersVO";
if(!srdto.getFname().equals("")) {
hqlQuery=hqlQuery+" where fname="+ "\'"+srdto.getFname()+"\'";
}
System.out.println(hqlQuery);
return HibernateTemplate.getAllRecords(hqlQuery);
}
}
step 5: Develop a HibernateSessionFactory.java file
package info.inetsolv.util;
import org.hibernate.cfg.*;
import org.hibernate.*;
public class HibernateSessionFactory {
private static SessionFactory sf = null;
static {
Configuration cfg = new Configuration();
cfg.configure();
System.out.println("*******configure() is called.....");
sf = cfg.buildSessionFactory();
System.out.println("*******buildSessionFactory() is called.....");
}
private HibernateSessionFactory() {
}
public static SessionFactory getSessionFactory() {
System.out.println("returning old sf Object");
return sf;
}
}
Step 6 : Develop a file HibernateTemplate.java file
package info.inetsolv.util;
import org.hibernate.cfg.*;
import org.hibernate.*;
import java.util.*;
public class HibernateTemplate {
public static void save(Object o) {
System.out.println("we are in HT save()");
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session hsession = sf.openSession();
Transaction tx = hsession.beginTransaction();
hsession.save(o) ;
tx.commit();
hsession.close();
}
public static void update(Object o) {
System.out.println("we are in HT update()");
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session hsession = sf.openSession();
Transaction tx = hsession.beginTransaction();
hsession.update(o) ;
tx.commit();
hsession.close();
}
public static ArrayList getAllRecords(String hqlQuery) {
System.out.println("we are in HT getAllRecords()");
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session hsession = sf.openSession();
Query query = hsession.createQuery(hqlQuery);
ArrayList list = (ArrayList)query.list();
hsession.close();
return list;
}
}
import org.hibernate.cfg.*;
import org.hibernate.*;
import java.util.*;
public class HibernateTemplate {
public static void save(Object o) {
System.out.println("we are in HT save()");
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session hsession = sf.openSession();
Transaction tx = hsession.beginTransaction();
hsession.save(o) ;
tx.commit();
hsession.close();
}
public static void update(Object o) {
System.out.println("we are in HT update()");
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session hsession = sf.openSession();
Transaction tx = hsession.beginTransaction();
hsession.update(o) ;
tx.commit();
hsession.close();
}
public static ArrayList getAllRecords(String hqlQuery) {
System.out.println("we are in HT getAllRecords()");
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session hsession = sf.openSession();
Query query = hsession.createQuery(hqlQuery);
ArrayList list = (ArrayList)query.list();
hsession.close();
return list;
}
}
Step 7 : Developing Hibernate Mapping File users.hbm.xml file
<?xml version="1.0"?><!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" PUBLIC "hm">
<hibernate-mapping>
<class table="users" name="info.inetsolv.vo.UsersVO">
<id name="fname"/>
<property name="lname"/>
<property name="email"/>
<property name="pwd"/>
<property name="dob"/>
<property name="gender"/>
</class>
</hibernate-mapping>
Step 8 : Develop aHibernate Configuration File hibernate.cfg.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" PUBLIC "hibernate-configuration"><hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:xe
</property>
<property name="connection.username">raja</property>
<property name="connection.password">janu</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<mapping resource="users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
JavaBean class: In this Java Bean class,we can create a Dao object and Call the method of Dao class .
package info.inetsolv.jb;
import info.inetsolv.dto.*;
import info.inetsolv.dao.*;
import java.util.*;
public class SearchUserJB {
public ArrayList searchUsers(SearchUserDTO sudto) {
SearchUserDAO sudao = new SearchUserDAO();
return sudao.searchUsers(sudto);
}
}
Step 9 : Develop a Action Class for creating DynavalidatorForm and JavaBean Objects and call the JavaBean method.
SearchUserAction.java
package info.inetsolv.action;import java.util.*;
import info.inetsolv.jb.*;
import info.inetsolv.dto.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.validator.*;
public class SearchUserAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
ActionForward af = null;
DynaValidatorForm dvf = (DynaValidatorForm)form;
SearchUserDTO sudto = new SearchUserDTO();
sudto.setFname((String)dvf.get("fname"));
SearchUserJB sujb = new SearchUserJB();
ArrayList list = sujb.searchUsers(sudto);
request.setAttribute("list",list);
return mapping.findForward("display");
}
}
Step 10: Finally Configure the Action class into Struts Configuratin File
a).configure FormBean Class
<form-beans>
<form-bean name="su" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="fname" type="java.lang.String"/>
</form-bean>
b).Action Class Configured in Struts Configuration File
<action-mappings>
<action path="/su" type="info.inetsolv.action.SearchUserAction" name="su" scope="request" input="/search.jsp">
<forward name="display" path="/display.jsp"/>
</action>
Step 11: Displaying Contents in display.jsp file
<%@ page isELIgnored="false"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<logic:empty name="list">
No Records available.<br>
</logic:empty>
<logic:iterate id="user" name="list">
<bean:write name="user" property="fname"/>
<bean:write name="user" property="email"/>
<bean:write name="user" property="dob"/>
<bean:write name="user" property="gender"/>
<br>
</logic:iterate>
<a href="search.jsp">Search.</a>
0 comments:
Post a Comment