测试页面:index.jsp
<%@ page language="Java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="hibernate.HibernateSessionFactory"%>
<%@ page language="java" import="mappingConfig.Admin"%>
<%@ page language="java" import="org.hibernate.HibernateException"%>
<%@ page language="java" import="org.hibernate.*"%>
<!DOCTYPE Html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<FORM action="log.do" method="post">
username
<input type="text" name="name">
<br>
passWord
<input type="password" name="password">
<br>
<input type="submit" value="login">
</FORM>
</body>
</html>
right.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
This is my JSP page. <br>
</body>
</html>
LogAction.java
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package action;
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 form.UserForm;
import common.Com;
/**
* MyEclipse Struts
* Creation date: 03-25-2006
*
* XDoclet definition:
* @struts.action path="/log" name="userForm" input="/index.jsp" scope="request" validate="true"
*/
public class LogAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserForm userForm = (UserForm) form;
// TODO Auto-generated method stub
Com.execute();
return mapping.findForward("sUC");
}
}
Com.java
package common;
import hibernate.HibernateSessionFactory;
import mappingConfig.Admin;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Com {
public static boolean execute() {
Session session;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Admin admin = new Admin();
admin.setName("joliny");
admin.setAge("21");
admin.setEmail("jbakwd@163.com");
admin.setPassword("2466346");
admin.setSex("man");
admin.setTelphone("13571944243");
session.save(admin);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
}
return true;
}
//public static void main(String args[]) {
//execute();
//}
}
UserForm.java
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 03-25-2006
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
// --------------------------------------------------------- Instance Variables
/** password property */
private String password;
/** name property */
private String name;
// --------------------------------------------------------- Methods
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the password.
* @return String
*/
public String getPassword() {
return password;
}
/**
* Set the password.
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}
/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
}
HibernateSessionFactory.java
package hibernate;
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.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
/**
* 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();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}
AbstractAdmin.java
/*
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*
* Created Fri Mar 24 14:48:52 GMT 2006 by MyEclipse Hibernate Tool.
*/
package mappingConfig;
import java.io.Serializable;
/**
* A class that represents a row in the admin table.
* You can customize the behavior of this class by editing the class, {@link Admin()}.
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*/
public abstract class AbstractAdmin
implements Serializable
{
/** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */
private int hashValue = 0;
/** The composite primary key value. */
private java.lang.Integer id;
/** The value of the simple name property. */
private java.lang.String name;
/** The value of the simple password property. */
private java.lang.String password;
/** The value of the simple telphone property. */
private java.lang.String telphone;
/** The value of the simple email property. */
private java.lang.String email;
/** The value of the simple sex property. */
private java.lang.String sex;
/** The value of the simple age property. */
private java.lang.String age;
/**
* Simple constructor of AbstractAdmin instances.
*/
public AbstractAdmin()
{
}
/**
* Constructor of AbstractAdmin instances given a simple primary key.
* @param id
*/
public AbstractAdmin(java.lang.Integer id)
{
this.setId(id);
}
/**
* Return the simple primary key value that identifies this object.
* @return java.lang.Integer
*/
public java.lang.Integer getId()
{
return id;
}
/**
* Set the simple primary key value that identifies this object.
* @param id
*/
public void setId(java.lang.Integer id)
{
this.hashValue = 0;
this.id = id;
}
/**
* Return the value of the name column.
* @return java.lang.String
*/
public java.lang.String getName()
{
return this.name;
}
/**
* Set the value of the name column.
* @param name
*/
public void setName(java.lang.String name)
{
this.name = name;
}
/**
* Return the value of the password column.
* @return java.lang.String
*/
public java.lang.String getPassword()
{
return this.password;
}
/**
* Set the value of the password column.
* @param password
*/
public void setPassword(java.lang.String password)
{
this.password = password;
}
/**
* Return the value of the telphone column.
* @return java.lang.String
*/
public java.lang.String getTelphone()
{
return this.telphone;
}
/**
* Set the value of the telphone column.
* @param telphone
*/
public void setTelphone(java.lang.String telphone)
{
this.telphone = telphone;
}
/**
* Return the value of the email column.
* @return java.lang.String
*/
public java.lang.String getEmail()
{
return this.email;
}
/**
* Set the value of the email column.
* @param email
*/
public void setEmail(java.lang.String email)
{
this.email = email;
}
/**
* Return the value of the sex column.
* @return java.lang.String
*/
public java.lang.String getSex()
{
return this.sex;
}
/**
* Set the value of the sex column.
* @param sex
*/
public void setSex(java.lang.String sex)
{
this.sex = sex;
}
/**
* Return the value of the age column.
* @return java.lang.String
*/
public java.lang.String getAge()
{
return this.age;
}
/**
* Set the value of the age column.
* @param age
*/
public void setAge(java.lang.String age)
{
this.age = age;
}
/**
* Implementation of the equals comparison on the basis of equality of the primary key values.
* @param rhs
* @return boolean
*/
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof Admin))
return false;
Admin that = (Admin) rhs;
if (this.getId() == null that.getId() == null)
return false;
return (this.getId().equals(that.getId()));
}
/**
* Implementation of the hashCode method conforming to the Bloch pattern with
* the exception of array properties (these are very unlikely primary key types).
* @return int
*/
public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
int idValue = this.getId() == null ? 0 : this.getId().hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
}
Admin.java
/*
* Created Fri Mar 24 14:31:50 GMT 2006 by MyEclipse Hibernate Tool.
*/
package mappingConfig;
import java.io.Serializable;
/**
* A class that represents a row in the 'admin' table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Admin
extends AbstractAdmin
implements Serializable
{
/**
* Simple constructor of Admin instances.
*/
public Admin()
{
}
/**
* Constructor of Admin instances given a simple primary key.
* @param id
*/
public Admin(java.lang.Integer id)
{
super(id);
}
/* Add customized code below */
}
配置文件:
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">webases</property>
<property name="connection.url">jdbc:mysql://localhost/weBTest</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property><mapping resource="mappingConfig/Admin.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
Admin.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" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Fri Mar 24 14:48:52 GMT 2006 -->
<hibernate-mapping package="mappingConfig">
<class name="Admin" table="admin">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" not-null="true" />
<property name="password" column="password" type="string" not-null="true" />
<property name="telphone" column="telphone" type="string" not-null="true" />
<property name="email" column="email" type="string" not-null="true" />
<property name="sex" column="sex" type="string" not-null="true" />
<property name="age" column="age" type="string" not-null="true" />
</class>
</hibernate-mapping>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="form.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
input="/index.jsp"
name="userForm"
path="/log"
scope="request"
type="action.LogAction" >
<forward name="suc" path="/right.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>