Struts + Hibernate Integration Example With MyEclipse
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<center><h1>Student Registration</h1><hr>
<html:form method="POST" action="/store">
<table bgcolor="#ccccff">
<tr><td>Student Name : </td><td><html:text property="name"></html:text></td></tr>
<tr><td>Course Name : </td><td><html:text property="course"></html:text></td></tr>
<tr><td>Faculty Name : </td><td><html:text property="fname"></html:text></td></tr>
<tr><td> </td><td><html:submit property="Submit">Register</html:submit><html:reset>Clear</html:reset></td></tr>
</table>
<br>
</html:form>
</center>
</body>
</html>
StudForm.java
package com.struts.hibernate;
import java.io.Serializable;
import org.apache.struts.action.ActionForm;
public class StudForm extends ActionForm implements Serializable{
private long sid;
private String name;
private String course;
private String fname;
public long getSid() {
return sid;
}
public void setSid(long sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
}
StudentAction.java
package com.struts.hibernate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;
import sf.HibernateSessionFactory;
public class StudentAction extends Action {
public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest req,HttpServletResponse res)throws Exception{
StudForm sform=(StudForm)af;
Session s=HibernateSessionFactory.getSession();
Transaction t=s.beginTransaction();
Long id;
id=(Long)s.save(sform);
req.setAttribute("sid", id);
t.commit();
return am.findForward("success");
}
}
stud.hbm.xml
<?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">
<hibernate-mapping>
<class name="com.struts.hibernate.StudForm" table="STUDENTS_DATA">
<id name="sid" column="SID" type="long">
<generator class="native"/>
</id>
<property name="name" type="string" column="NAME"/>
<property name="course" column="COURSE" type="string"/>
<property name="fname" column="FACULTY" type="string"/>
</class>
</hibernate-mapping>
HibernateSessionFactory.java
package sf;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
static {
try {
configuration.configure("/hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static Session getSession() throws HibernateException {
Session session =sessionFactory.openSession();
return session;
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
}
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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/java</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/struts/hibernate/stud.hbm.xml"/>
</session-factory>
</hibernate-configuration>
ViewStudentAction.java
package com.struts.hibernate;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;
import sf.HibernateSessionFactory;
public class ViewStudentAction extends Action {
public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest req,HttpServletResponse res)throws Exception{
StudForm sform=(StudForm)af;
Session s=HibernateSessionFactory.getSession();
Transaction t=s.beginTransaction();
List sdata=s.createQuery("from StudForm").list();
//Object stud1=s.get(StudForm.class, new Long(sform.getSid()));
req.setAttribute("sdata", sdata);
t.commit();
return am.findForward("success");
}
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans >
<form-bean name="st" type="com.struts.hibernate.StudForm"></form-bean>
</form-beans>
<action-mappings >
<action path="/store" name="st" type="com.struts.hibernate.StudentAction">
<forward name="success" path="/result.jsp"></forward>
</action>
<action path="/view" name="st" type="com.struts.hibernate.ViewStudentAction">
<forward name="success" path="/show.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.struts.hibernate.ApplicationResources" />
</struts-config>
result.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>result page</title> </head>
<body> <center>
<font color="Blue">
<h1>Thank You....... </h1><br>
<h2>Student Registered Successfully with SID : <%=request.getAttribute("sid") %> </h2>
</font>
<br>
<H3><html:link action="/view">View All Details</html:link></H3>
</center>
</body>
</html:html>
show.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.List"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head> <html:base />
<title>show details</title> </head>
<body bgcolor="#ccccff"> <center>
<table border="1">
<tr bgcolor="#ffcc66">
<th>StudentID</th>
<th>StudentName</th>
<th>StudentCourse</th>
<th>FacultyName</th>
</tr>
<logic:iterate id="s" name="sdata">
<tr bgcolor="#ccffff">
<td><bean:write name="s" property="sid"/></td>
<td><bean:write name="s" property="name"/></td>
<td><bean:write name="s" property="course"/></td>
<td><bean:write name="s" property="fname"/></td>
</tr>
</logic:iterate>
</table>
</center>
</body>
</html:html>