<%@ page contentType="text/html; charset=gb2312"%> Struts+hibernate开发(源代码)
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
Struts+hibernate开发(源代码)
     发布者: 发布时间:2007-01-05
测试页面: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>
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
Java 平台透析
Struts + Hibernate 代码生成器设计思路
Eclipse基础--plugin插件安装
Java程序员面试三十二问
如何安装配置你的Tomcat5并绑定域名
安装配置Jboss完全篇
 
最近评论:
        
冰封的往事!
wow power leveling,wow gold,wow power leveling,wow gold max(9784)
        
飞舞的传奇!
传世私服,传世私服.传奇世界私服传奇世界私服,传世私服传世私服, 传奇世界私服传奇世界私服.传奇私服传奇私服. max(851)
        
Timmy
b2e708fe06984833ae4cff720c799834 http://date-nascita-affinita-coppia.ygvhik.biz/ http://volo-nizza-venezia.zibtye.biz/ http://centro-benessere-regione-sardegna.kajgdw.biz/ http://commissioni-esami.iuatju.biz/ http://problema-donna-velo.ygvhik.biz/ http://donne-impresa.ppdpwx.biz/ http://trucchi-travian.tzlnou.biz/ http://scaricare-gratis-visual-basic-pro.iuatju.biz/ http://17-rgt-acqui-capua.kajgdw.biz/ http://cannero-riviera.ygvhik.biz/ 69fae163d26a9b1682339a4eb6fc4ad9
        
Cole
8ad4e161199f3c75ca7cd82bfb0312c6 http://centro-storico-palermo-residenti.vywyuh.org/ http://scopami-zio.gbdrme.org/ http://sblocco-telefono-tre-nokia-6680.gbdrme.org/ http://motel-autostradale.jnbwct.org/ http://scuola-guida-napoli.mnkcbe.org/ http://composizioni-musicali.jnbwct.org/ http://agenzia-comunicazione-marketing.vywyuh.org/ http://lucia-galeone-oops.hdpwsk.org/ http://progetto-obiettivo-polizia-municipale.cqhnnx.org/ http://selen-con-salieri.hdpwsk.org/ eb89aa2351bfb8dd061b0dc25061dcdb
        
Aden
0f8f72ff6c58bbc0260a7c77c9612693 MosheHadnott BraedonTorrens DustyGinn RoxasHouston PargaAmos WalkerSwart JudahSimard BraedonTorrens MarvinBirden IsaiahHugh 1bb5907b91a875f331998f8e4f2b0572
        
Joseluis
cd50d639894a70ed28f9eeb98089cb93 sanzione imposta evasa irpef scuola ballo ola guelfo y anna il mammo cast lettera comunicazione ai genitore padrona amanda it congelare verdura cotta pablo picasso palazzo grasso abitare fronte ferrovia milano lavoro annuale erotico spagnoli b8fb7d84153cc5c69600cbe1497734b2
        
Arsenio
920700b397e8be867c6ce1aa005116bd http://gli-specchi.licoxi.in/ http://edera-pianta.fzhoas.in/ http://oggetti-in-ferro.licoxi.in/ http://traghetto-pescara-medjugorie.jnesky.in/ http://lamina-rasoio-braun-8995.aezqpa.com/ http://paesaggio.fzhoas.in/ http://vedere-carta-d-identita-fac-simile.qeshtw.in/ http://ulisse-pdf-joyce-download.kvpzig.com/ http://samba-linux-non-parte.aoknmm.in/ http://vino-bianchi-sardegna.iznvge.in/ b8a12f78e2ab8d9c8e5e94f78e975725
        
Hunter
http://13c8fd150134fd11ed4b4fecd421c2a2-t.xkktxb.org 13c8fd150134fd11ed4b4fecd421c2a2 [url]http://13c8fd150134fd11ed4b4fecd421c2a2-b1.xkktxb.org[/url] [url=http://13c8fd150134fd11ed4b4fecd421c2a2-b2.xkktxb.org]13c8fd150134fd11ed4b4fecd421c2a2[/url] [u]http://13c8fd150134fd11ed4b4fecd421c2a2-b3.xkktxb.org[/u] 8d1f2bfe3cbc5359328d95464cab8b7c
        
Marco
a56321b1696b04140a94bc2ba60f3612 ultima scoperte luna annuncio moto usata cagiva master in economia e finanza agriturismo sasso rosso mostro lochness calza porno fee animale olfatto zoccolo milan roma 11 novembre piacenza cavalcavia progetto sopra ferrovia foto ballerina sopravento 8ea4fcdde1a965ef95e68187f350c6f6
        
Wilson
fa19af19a619fd18fbb4547f652ff0c3 http://t-shirt-donna-alviero-martini.mxkrxs.com/ http://vito-elio-petrucci.knhtou.com/ http://sala-ballo-torino.xmjviq.com/ http://gioco-carta-cavaliere-zodiaco.ywowql.com/ http://joia-discoteca-roma.jmncsw.biz/ http://sulla-vecchiaia.ynpojb.biz/ http://honky-tonky-train-blues.xmjviq.com/ http://fumetto-dragon-ball.knhtou.com/ http://apparato-digerente-orso.jmncsw.biz/ http://magazzino-lafayette.drncar.biz/ 8cff813cd5cdf93d908a9e43c4704dad
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号