jars required
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.18.jar
ognl-3.0.4.jar
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
commons-lang-2.5.jar
javassist-3.11.0.GA.jar
struts2-dojo-plugin-2.3.1.2.jar
antlr-2.7.6.jar
commons-collection-3.1.jar
dom4j-1.6.1.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
hibernate3.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
mysql-connector-java-5.1.6.jar
Directory Structure
create table dept(
deptno INT PRIMARY KEY AUTO_INCREMENT,
dname varchar(50),
loc varchar(50)
);
web.xml
Department.java
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.18.jar
ognl-3.0.4.jar
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
commons-lang-2.5.jar
javassist-3.11.0.GA.jar
struts2-dojo-plugin-2.3.1.2.jar
antlr-2.7.6.jar
commons-collection-3.1.jar
dom4j-1.6.1.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
hibernate3.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
mysql-connector-java-5.1.6.jar
Directory Structure
create table dept(
deptno INT PRIMARY KEY AUTO_INCREMENT,
dname varchar(50),
loc varchar(50)
);
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>Struts2.3_HibernateIntegration</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Department Details Maintenance</title>
</head>
<body>
<h1>Department Details</h1>
<s:actionerror />
<s:form action="add" method="post">
<s:textfield name="department.departmentNo" label="Department No" />
<s:textfield name="department.departmentName" label="Department Name" />
<s:textfield name="department.location" label="Department Location" />
<s:submit value="Add Deparment" align="center" />
</s:form>
<h2>Departments</h2>
<table>
<tr>
<th>No</th>
<th>Name</th>
<th>Location</th>
<th>Delete</th>
</tr>
<s:iterator value="departmentList" var="department">
<tr>
<td><s:property value="departmentNo" />
<td><s:property value="departmentName" /></td>
<td><s:property value="location" /></td>
<td><a
href="delete?departmentNo=<s:property value="departmentNo"/>">delete</a></td>
</tr>
</s:iterator>
</table>
</body>
</html>
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="true" />
<constant name="struts.devMode" value="false" />
<include file="struts-default"></include>
<package name="default" extends="struts-default" namespace="/">
<action name="add" class="com.usr.view.DepartmentAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>
</action>
<action name="delete" class="com.usr.view.DepartmentAction"
method="delete">
<result name="success" type="chain">index</result>
</action>
<action name="index" class="com.usr.view.DepartmentAction">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
<!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="true" />
<constant name="struts.devMode" value="false" />
<include file="struts-default"></include>
<package name="default" extends="struts-default" namespace="/">
<action name="add" class="com.usr.view.DepartmentAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>
</action>
<action name="delete" class="com.usr.view.DepartmentAction"
method="delete">
<result name="success" type="chain">index</result>
</action>
<action name="index" class="com.usr.view.DepartmentAction">
<result name="success">index.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://localhost:3306/hibernate_practice</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.usr.model.Department"/>
<!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
<mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
<!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://localhost:3306/hibernate_practice</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.usr.model.Department"/>
<!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
<mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
Department.java
package com.usr.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "dept")
public class Department implements Serializable {
private int departmentNo;
private String departmentName;
private String location;
@Id
@Column(name = "deptno")
public int getDepartmentNo() {
return departmentNo;
}
public void setDepartmentNo(int departmentNo) {
this.departmentNo = departmentNo;
}
@Column(name = "dname")
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Column(name = "loc")
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "dept")
public class Department implements Serializable {
private int departmentNo;
private String departmentName;
private String location;
@Id
@Column(name = "deptno")
public int getDepartmentNo() {
return departmentNo;
}
public void setDepartmentNo(int departmentNo) {
this.departmentNo = departmentNo;
}
@Column(name = "dname")
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Column(name = "loc")
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
HibernateUtil.java
package com.usr.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
DepartmentManager.java
package com.usr.controller;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import com.usr.model.Department;
import com.usr.util.HibernateUtil;
public class DepartmentManager extends HibernateUtil {
public Department add(Department department) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
System.err.println("session"+session);
session.beginTransaction();
session.save(department);
session.getTransaction().commit();
return null;
}
public Department delete(Integer departmentNo) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Department department = (Department) session.load(Department.class,
departmentNo);
if (null != department) {
session.delete(department);
}
session.getTransaction().commit();
return null;
}
public List<Department> list() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Department> departmentList = null;
try {
departmentList = (List<Department>) session.createQuery(
" from Department ").list();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
// session.getTransaction().commit();
return departmentList;
}
}
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import com.usr.model.Department;
import com.usr.util.HibernateUtil;
public class DepartmentManager extends HibernateUtil {
public Department add(Department department) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
System.err.println("session"+session);
session.beginTransaction();
session.save(department);
session.getTransaction().commit();
return null;
}
public Department delete(Integer departmentNo) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Department department = (Department) session.load(Department.class,
departmentNo);
if (null != department) {
session.delete(department);
}
session.getTransaction().commit();
return null;
}
public List<Department> list() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Department> departmentList = null;
try {
departmentList = (List<Department>) session.createQuery(
" from Department ").list();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
// session.getTransaction().commit();
return departmentList;
}
}
DepartmentAction.java
package com.usr.view;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.usr.controller.DepartmentManager;
import com.usr.model.Department;
public class DepartmentAction extends ActionSupport {
private DepartmentManager departmentManager;
private Department department;
private List<Department> departmentList;
private Integer departmentNo;
public DepartmentAction() {
departmentManager = new DepartmentManager();
}
public String execute() {
System.err.println("Execute");
// if (null != department) {
// departmentManager.add(getDepartment());
// }
this.departmentList = departmentManager.list();
return SUCCESS;
}
public String add() {
departmentManager.add(getDepartment());
return SUCCESS;
}
public String delete() {
departmentManager.delete(getDepartmentNo());
return SUCCESS;
}
public DepartmentManager getDepartmentManager() {
return departmentManager;
}
public void setDepartmentManager(DepartmentManager departmentManager) {
this.departmentManager = departmentManager;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public List<Department> getDepartmentList() {
return departmentList;
}
public void setDepartmentList(List<Department> departmentList) {
this.departmentList = departmentList;
}
public Integer getDepartmentNo() {
return departmentNo;
}
public void setDepartmentNo(Integer departmentNo) {
this.departmentNo = departmentNo;
}
}
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.usr.controller.DepartmentManager;
import com.usr.model.Department;
public class DepartmentAction extends ActionSupport {
private DepartmentManager departmentManager;
private Department department;
private List<Department> departmentList;
private Integer departmentNo;
public DepartmentAction() {
departmentManager = new DepartmentManager();
}
public String execute() {
System.err.println("Execute");
// if (null != department) {
// departmentManager.add(getDepartment());
// }
this.departmentList = departmentManager.list();
return SUCCESS;
}
public String add() {
departmentManager.add(getDepartment());
return SUCCESS;
}
public String delete() {
departmentManager.delete(getDepartmentNo());
return SUCCESS;
}
public DepartmentManager getDepartmentManager() {
return departmentManager;
}
public void setDepartmentManager(DepartmentManager departmentManager) {
this.departmentManager = departmentManager;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public List<Department> getDepartmentList() {
return departmentList;
}
public void setDepartmentList(List<Department> departmentList) {
this.departmentList = departmentList;
}
public Integer getDepartmentNo() {
return departmentNo;
}
public void setDepartmentNo(Integer departmentNo) {
this.departmentNo = departmentNo;
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.