测试页面: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>
闽公网安备 35060202000074号