Hibernate integrate with Struts - select records from table
DataBase Design
Database Name: Hiber
Table Table: hiber
Step 1:
Create Database.
mysql > create database Hiber;
Step 2:
Create Table
Mysql > create table hiber(stuname varchar(40),stuno int,primary key(stuno));
I have inserted into two records into hiber table.
Step 1). How to create Project for Hibernate Struts integration.
1.1) Open netbean 6.9
1.2) Go to File – New Project(ctrl+shift+N).
New Project window will open.
Where we select
java web – under Categories |
Web Application—under Projects |
Next
New web Application window will open.
Where we give Project name. I have given project name as HibernateStruts
Next
New window will open. Where we select server. I have select Apache Tomcat 6.0.26
Next
New window will open
Where we select framework.
Here we select two frameworks. Because we integrate Hibernate with Struts.
Step 1:
Select Hibernate 3.2.5 in framework
Below Hibernate 3.2.5 Configuration
Select New Database Connection
After select New Database Connection
New Database Connection window will open.
Here we made some changes.
Driver Name: MySQL(Connector/J Driver)
Host : localhost
Database : hiber
User name : root
Password : sure
Display Name(Optional) : HiberDB
Select show JDBC URL
OK
You can see.
Below Hibernate 3.2.5 Configuration
Database Connection: HiberDB
Step 2:
Select Struts 1.3.8 in framework
Below Struts 1.3.8 Configuration
Application Resource : Resources.ApplicationResource
Select Add Struts TLDs
Screen shot
Finish
Successfully Project Created.
Note: |
We delete two jsp files such as index.jsp and welcomeStruts.jsp |
************************************************************************
Step 2:
2.1). Folder Creation for storing jsp files.
Right click on web pages and Select New--Folder
New Folder window will open. Where we give Folder name as jsp.
Finish.
Folder named as jsp have been created successfully.
2.2). Folder Creation for Hibernate files named as Hiber
Folder name is Hiber.
Right Click on Source packages
New Folder window will open.
Finish
Hiber Folder for Hibernate successfully created.
2.3). Create Another Folder for ActionForm for Struts
named as ActionForms
Follow previous step to create Folder
2.4). Create another Folder for Action for struts
named as Actions
2.5). Create another Folder for Plugin for java class.
Named as Plugin
Step: 3
Creating ActionForms.
How to create ActionForm in Struts |
Before creating ActionForms, we should analyze the property we need to create ActionForms.
I have used two property one is studentname and another one is studentno.
I have given actionform name as StudentForms
Insert the following coding into StudentForms
package ActionForms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class StudentForms extends org.apache.struts.action.ActionForm
{
private String studentname;
private int studentno;
public String getStudentname()
{
return studentname;
}
public void setStudentname(String studentname)
{
this.studentname = studentname;
}
public int getStudentno()
{
return studentno;
}
public void setStudentno(int studentno)
{
this.studentno = studentno;
}
}
Step 4:
Create Plugin.
I have given name for Plugin java as HibernatePlugin.java
Include following coding into HibernatePlugin.java
package Plugin;
import java.net.URL;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernatePlugin implements PlugIn
{
private Configuration config;
private SessionFactory factory;
private String path = "/hibernate.cfg.xml";
private static Class clazz = HibernatePlugin.class;
public static final String KEY_NAME = clazz.getName();
public void setPath(String path)
{
this.path = path;
}
public void init(ActionServlet servlet, ModuleConfig modConfig)
throws ServletException {
try {
//save the Hibernate session factory into serlvet context
URL url = HibernatePlugin.class.getResource(path);
config = new Configuration().configure(url);
factory = config.buildSessionFactory();
servlet.getServletContext().setAttribute(KEY_NAME, factory);
} catch (MappingException e) {
throw new ServletException();
} catch (HibernateException e) {
throw new ServletException();
}
}
public void destroy() {
try {
factory.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
Screen Shot
Step 5:
Step 5:
Step 6:
We have to create all files for Hibernate.
Such as Reverse Engineering and HibernateUtil and POJO class and Mapping files.
Step 6.1:
Create Hibernate Reverse Engineering wizard
Step 6.2:
Create HibernateUtil.java
Step 6.3:
Create POJO class and mapping files
How to create these files? Click
Step 7:
Creating Action
I have given name as StudentAction
Include following coding into StudentAction
package Actions;
import ActionForms.StudentForms;
import Plugin.HibernatePlugin;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.SessionFactory;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.Criteria.*;
import Hiber.Hiber;
import java.util.List;
public class StudentAction extends org.apache.struts.action.Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
StudentForms sform=(StudentForms)form;
SessionFactory sessionFactory = (SessionFactory) servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
Session session=sessionFactory.openSession();
Criteria crit=session.createCriteria(Hiber.class);
List list=crit.list();
request.setAttribute("slist",list);
return mapping.findForward("success");
}
}
Step 8:
Creating jsp files.
Step 8.1).
Home.jsp
Include following coding into Home.jsp
< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html >
< head >
< title > JSP Page < /title >
< /head >
< body >
< html:form action="student" method="post" >
< html:submit value="OK"/ >
< /html:form >
< /body >
< /html >
Step 8.2).
To get records from Database
Create another jsp file named success.jsp
< %@page import="Hiber.Hiber"% >
< %@page import="ActionForms.StudentForms"% >
< %@page import="java.util.*"% >
< html >
< head >
< title > Hibernate with Struts Example < /title >
< /head >
< body >
< %
List list=(List)request.getAttribute("slist");
out.println(list.size());
Iterator it=list.iterator();
while(it.hasNext())
{
Hiber h=(Hiber)it.next();
out.println(h.getStuname());
out.println(h.getStuno());
}
% >
< /body >
< /html >
Step 9:
Made some changes in Struts-config.xml
- - - - -
< action-mappings >
< action
input="/"
name="StudentForms"
path="/student"
scope="session"
type="Actions.StudentAction" >
< forward name="success" path="/jsp/success.jsp"/ >
< /action >
< /action-mappings >
- - - - -
Step 10:
Run Home.jsp
We have learned how to integrate Hibernate with Struts from this post.
Thank you .
By Yours
S.Sureshkumar, M.Sc, M.Phil,
No comments:
Post a Comment