1 OpenAction.java
/**/
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package
org.zhb.struts.action;

import
java.lang.reflect.InvocationTargetException;
import
java.util.ArrayList;

import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;

import
org.apache.commons.beanutils.BeanUtils;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.action.DynaActionForm;
import
org.apache.struts.actions.DispatchAction;
import
org.hibernate.Session;
import
org.hibernate.Transaction;

import
util.BaseHibernateDAO;
import
util.HibernateSessionFactory;
import
util.Student;
import
util.StudentDAO;


/** */
/**
* MyEclipse Struts
* Creation date: 05-29-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/

public
class
OpenAction
extends
DispatchAction
...
{

/**//*
* Generated Methods
*/


/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward insert(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) ...{
DynaActionForm operationForm = (DynaActionForm) form;
Student stu=new Student();

try ...{
BeanUtils.copyProperties(stu,operationForm);
BaseHibernateDAO bao=new BaseHibernateDAO();
Session session=bao.getSession();
Transaction tr=session.beginTransaction();
StudentDAO dao=new StudentDAO();
dao.save(stu);
tr.commit();
session.close();

} catch (IllegalAccessException e) ...{
e.printStackTrace();

} catch (InvocationTargetException e) ...{
e.printStackTrace();
}
return mapping.findForward("find");
}
public ActionForward find(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) ...{
StudentDAO dao=new StudentDAO();
ArrayList list=dao.findAll();
request.setAttribute("list",list);
return mapping.findForward("display");
}
public ActionForward delete(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) ...{
DynaActionForm operationForm=(DynaActionForm)form;
Long id=(Long)operationForm.get("id");
Session session=HibernateSessionFactory.getSession();
Transaction tr=session.beginTransaction();
StudentDAO dao=new StudentDAO();
dao.deleteById(id);
tr.commit();
session.close();
return mapping.findForward("find");
}
public ActionForward modify(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) ...{
DynaActionForm operationForm=(DynaActionForm)form;
Student stu=new Student();

try ...{
BeanUtils.copyProperties(stu,operationForm);
BaseHibernateDAO bao=new BaseHibernateDAO();
Session session=bao.getSession();
Transaction tr=session.beginTransaction();
StudentDAO dao=new StudentDAO();
dao.modifybyID(stu);
tr.commit();
session.close();

} catch (IllegalAccessException e) ...{
e.printStackTrace();

} catch (InvocationTargetException e) ...{
e.printStackTrace();
}
return mapping.findForward("find");
}
}
2 BaseHibernateDAO.java
package
util;

import
org.hibernate.Session;



/** */
/**
* Data access object (DAO) for domain model
* @author MyEclipse - Hibernate Tools
*/

public
class
BaseHibernateDAO
implements
IBaseHibernateDAO
...
{

public Session getSession() ...{
return HibernateSessionFactory.getSession();
}
}
3 HibernateSessionFactory.java
package
util;

import
org.hibernate.HibernateException;
import
org.hibernate.Session;
import
org.hibernate.cfg.Configuration;


/** */
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/

public
class
HibernateSessionFactory
...
{


/** *//**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;


private HibernateSessionFactory() ...{
}

/** *//**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/

public static Session getSession() throws HibernateException ...{
Session session = (Session) threadLocal.get();


if (session == null || !session.isOpen()) ...{

if (sessionFactory == null) ...{
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}


/** *//**
* Rebuild hibernate session factory
*
*/

public static void rebuildSessionFactory() ...{

try ...{
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();

} catch (Exception e) ...{
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}


/** *//**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/

public static void closeSession() throws HibernateException ...{
Session session = (Session) threadLocal.get();
threadLocal.set(null);


if (session != null) ...{
session.close();
}
}


/** *//**
* return session factory
*
*/

public static org.hibernate.SessionFactory getSessionFactory() ...{
return sessionFactory;
}


/** *//**
* return session factory
*
* session factory will be rebuilded in the next call
*/

public static void setConfigFile(String configFile) ...{
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}


/** *//**
* return hibernate configuration
*
*/

public static Configuration getConfiguration() ...{
return configuration;
}

}
4 IBaseHibernateDAO.java
package
util;

import
org.hibernate.Session;



/** */
/**
* Data access interface for domain model
* @author MyEclipse - Hibernate Tools
*/

public
interface
IBaseHibernateDAO
...
{
public Session getSession();
}
5 Student.java
package
util;




/** */
/**
* Student generated by MyEclipse - Hibernate Tools
*/


public
class
Student
implements
java.io.Serializable
...
{


// Fields

private Long id;
private String name;


// Constructors


/** *//** default constructor */

public Student() ...{
}


/** *//** full constructor */

public Student(String name) ...{
this.name = name;
}

// Property accessors


public Long getId() ...{
return this.id;
}

public void setId(Long id) ...{
this.id = id;
}


public String getName() ...{
return this.name;
}

public void setName(String name) ...{
this.name = name;
}








}
6 StudentDAO.java
package
util;

import
java.util.ArrayList;
import
java.util.List;

import
javax.swing.text.DefaultEditorKit.CutAction;

import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.hibernate.Criteria;
import
org.hibernate.LockMode;
import
org.hibernate.Query;
import
org.hibernate.Session;
import
org.hibernate.criterion.Criterion;
import
org.hibernate.criterion.Example;
import
org.hibernate.criterion.Expression;
import
org.hibernate.criterion.Order;


/** */
/**
* Data access object (DAO) for domain model class Student.
* @see util.Student
* @author MyEclipse - Hibernate Tools
*/

public
class
StudentDAO
extends
BaseHibernateDAO
...
{

private static final Log log = LogFactory.getLog(StudentDAO.class);

//property constants
public static final String NAME = "name";

//增加

public void save(Student transientInstance) ...{
log.debug("saving Student instance");

try ...{
getSession().save(transientInstance);
log.debug("save successful");

} catch (RuntimeException re) ...{
log.error("save failed", re);
throw re;
}
}

public void delete(Student persistentInstance) ...{
log.debug("deleting Student instance");

try ...{
getSession().delete(persistentInstance);
log.debug("delete successful");

} catch (RuntimeException re) ...{
log.error("delete failed", re);
throw re;
}
}
//通过ID删除

public void deleteById(Long id)...{
String querysql="delete from Student as s where s.id=?";
Query queryObject=getSession().createQuery(querysql);
queryObject.setParameter(0, id);
queryObject.executeUpdate();
}
//查询所有

public ArrayList findAll()...{
// ArrayList list=null;
// list=(ArrayList)getSession().createQuery("from Student").list();
Criteria criteria = getSession().createCriteria(Student.class);
Criterion criterion1 = Expression.like("id",new Integer("t%"));
Criterion criterion2 = Expression.like("name","t%");
criteria = criteria.add(criterion1) ;
criteria = criteria.add(criterion2) ;
ArrayList result = (ArrayList) criteria.list();
return result;
}

public void modify(Student persistentInstance)...{
getSession().update(persistentInstance);
}
//通过ID修改

public void modifybyID(Student persistentInstance)...{
String hql="update Student as s set s.name=? where s.id=?";
Query queryObject=getSession().createQuery(hql);
queryObject.setParameter(0, persistentInstance.getName());
queryObject.setParameter(1, persistentInstance.getId());
queryObject.executeUpdate();
}

public Student findById( java.lang.Long id) ...{
log.debug("getting Student instance with id: " + id);

try ...{
Student instance = (Student) getSession()
.get("util.Student", id);
return instance;

} catch (RuntimeException re) ...{
log.error("get failed", re);
throw re;
}
}

public List findByExample(Student instance) ...{
log.debug("finding Student instance by example");

try ...{
List results = getSession()
.createCriteria("util.Student")
.add(Example.create(instance))
.list();
log.debug("find by example successful, result size: " + results.size());
return results;

} catch (RuntimeException re) ...{
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) ...{
log.debug("finding Student instance with property: " + propertyName
+ ", value: " + value);

try ...{
String queryString = "from Student as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();

} catch (RuntimeException re) ...{
log.error("find by property name failed", re);
throw re;
}
}


public List findByName(Object name) ...{
return findByProperty(NAME, name);
}

public Student merge(Student detachedInstance) ...{
log.debug("merging Student instance");

try ...{
Student result = (Student) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;

} catch (RuntimeException re) ...{
log.error("merge failed", re);
throw re;
}
}


public void attachDirty(Student instance) ...{
log.debug("attaching dirty Student instance");

try ...{
getSession().saveOrUpdate(instance);
log.debug("attach successful");

} catch (RuntimeException re) ...{
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Student instance) ...{
log.debug("attaching clean Student instance");

try ...{
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");

} catch (RuntimeException re) ...{
log.error("attach failed", re);
throw re;
}
}
}
7 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
>
<
data-sources
/>
<
form-beans
>
<
form-bean
name
="openForm"
type
="org.apache.struts.action.DynaActionForm"
>
<
form-property
name
="id"
type
="java.lang.Long"
/>
<
form-property
name
="name"
type
="java.lang.String"
/>
</
form-bean
>
</
form-beans
>
<
global-exceptions
/>
<
global-forwards
/>
<
action-mappings
>
<
action
attribute
="openForm"
input
="/insert.jsp"
name
="openForm"
scope
="request"
parameter
="model"
path
="/open"
type
="org.zhb.struts.action.OpenAction"
>
<
forward
name
="display"
path
="/find.jsp"
/>
<
forward
name
="find"
path
="/open.do?model=find"
/>
</
action
>

</
action-mappings
>

<
message-resources
parameter
="org.zhb.struts.ApplicationResources"
/>
</
struts-config
>

8 delete.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
>
delete.jsp
</
title
>

<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
<
meta http
-
equiv
=
"
keywords
"
content
=
"
keyword1,keyword2,keyword3
"
>
<
meta http
-
equiv
=
"
description
"
content
=
"
This is my page
"
>
<!--
<
link rel
=
"
stylesheet
"
type
=
"
text/css
"
href
=
"
styles.css
"
>
-->

</
head
>
<
body
>
<
center
>
<
html:form action
=
"
/open.do?model=delete
"
>
<
table
>
<
tr
>
<
td
>
id
<
html:text property
=
"
id
"
></
html:text
>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
html:submit
></
html:submit
>
</
td
>
</
tr
>
</
table
>
</
html:form
>
</
center
>
</
body
>
</
html:html
>
9 find.jsp
<%
@ page contentType
=
"
text/html; charset=GBK
"
%>

<%
@ 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
>
find.jsp
</
title
>

<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
<
meta http
-
equiv
=
"
keywords
"
content
=
"
keyword1,keyword2,keyword3
"
>
<
meta http
-
equiv
=
"
description
"
content
=
"
This is my page
"
>
<!--
<
link rel
=
"
stylesheet
"
type
=
"
text/css
"
href
=
"
styles.css
"
>
-->

</
head
>
<
body
>
<
center
>
<
logic:present name
=
"
list
"
>
<
table
>
<
tr
>
<
td
>
id
</
td
>
<
td
>
name
</
td
>
</
tr
>
<
logic:iterate id
=
"
bean
"
name
=
"
list
"
>
<
tr
>
<
td
>
<
bean:write name
=
"
bean
"
property
=
"
id
"
/>
</
td
>
<
td
>
<
bean:write name
=
"
bean
"
property
=
"
name
"
/>
</
td
>
</
tr
>
</
logic:iterate
>
</
table
>
</
logic:present
>
<
BR
/>
<
html:link page
=
"
/insert.jsp
"
>
增加
</
html:link
>
<
html:link page
=
"
/delete.jsp
"
>
删除
</
html:link
>
<
html:link page
=
"
/modify.jsp
"
>
修改
</
html:link
>
</
center
>
</
body
>
</
html:html
>
10 insert.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
>
insert.jsp
</
title
>

<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
<
meta http
-
equiv
=
"
keywords
"
content
=
"
keyword1,keyword2,keyword3
"
>
<
meta http
-
equiv
=
"
description
"
content
=
"
This is my page
"
>
<!--
<
link rel
=
"
stylesheet
"
type
=
"
text/css
"
href
=
"
styles.css
"
>
-->

</
head
>
<
body
>
<
center
>
<
html:form action
=
"
/open.do?model=insert
"
>
<
table
>
<
tr
>
<
td
>
name
<
html:text property
=
"
name
"
/>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
html:submit
></
html:submit
>
</
td
>
</
tr
>
</
table
>
</
html:form
>
</
center
>
</
body
>
</
html:html
>