分享
 
 
 

Struts+hibernate开发(源代码)

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

测试页面: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>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有