Hibernate3+Struts2简单案例

代码总览

Hibernate3+Struts2简单案例_第1张图片

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>/xt/register.jsp</welcome-file>
	</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="xt" namespace="/xt" extends="struts-default">
        <action name="register" class="com.itlwc.action.Register">
        	<result name="success">/xt/registerSuccess.jsp</result>
        </action>
    </package>
</struts>
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">
<hibernate-configuration>
    <session-factory>
    	<!-- 
    		Hibernate连接SqlServer2000数据库需要注意的问题
    		必须通过jTDS驱动连接
    		下载地址:http://sourceforge.net/projects/jtds/files/jtds/
    	 -->
    	<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=itlwc</property>
    	<property name="connection.username">sa</property>
    	<property name="connection.password">sa</property>
    	<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
    	<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    	<property name="show_sql">true</property>
    	<mapping resource="Student_hbm.xml"/>
    </session-factory>
</hibernate-configuration>
student_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">
<hibernate-mapping>
	<class name="com.itlwc.entity.Student" table="d_student">
		<id name="id" column="id" type="int">
			<generator class="increment"></generator>
		</id>
		<property name="username" column="username" type="string"></property>
		<property name="password" column="password" type="string"></property>
		<property name="age" column="age" type="int"></property>
		<property name="date" column="data" type="date"></property>
	</class>
</hibernate-mapping>

register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<html>
	<head>
		<base href="<%=basePath%>">
		<script type="text/javascript">
			function doSubmit(){
				registerForm.action="<%=basePath%>xt/register"; 
				registerForm.submit();  
			}
		</script>
	</head>
	<body>
		<form name="registerForm" method="post">
			<table>
				<tr>
					<td>账号:</td>
					<td><input type="text" name='username'/></td>
				</tr>
				<tr>
					<td>密码:</td>
					<td><input type="text" name='password'/></td>
				</tr>
				<tr>
					<td>年龄:</td>
					<td><input type="text" name='age'/></td>
				</tr>
				<tr>
					<td colspan="2"><input type="button" value="注册" onclick="doSubmit()"/></td>
				</tr>
			</table>
		</form>
	</body>
</html>

Register.java

package com.itlwc.action;

import java.sql.Date;

import com.itlwc.entity.Student;
import com.itlwc.service.StudentService;
import com.itlwc.serviceImp.StudentServiceImp;
import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private int age;
	private Date date;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String execute() throws Exception {
		Student stu = new Student();
		stu.setUsername(username);
		stu.setPassword(password);
		stu.setAge(age);
		java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
		stu.setDate(date);
		StudentService ss = new StudentServiceImp();
		ss.saveStudent(stu);
		return SUCCESS;
	}
}

Student.java

package com.itlwc.entity;

import java.sql.Date;

public class Student {
	private int id;
	private String username;
	private String password;
	private int age;
	private Date date;

	public int getId() {
		return id;
	}

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

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}

StudentDao.java

package com.itlwc.dao;

import com.itlwc.entity.Student;

public interface StudentDao {
	public void saveStudent(Student stu);
}
StudentDaoImp.java
package com.itlwc.daoImp;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.itlwc.dao.StudentDao;
import com.itlwc.entity.Student;
import com.itlwc.utils.HibernateUtils;

public class StudentDaoImp implements StudentDao {
	public void saveStudent(Student stu) {
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(stu);
			tx.commit();
		} catch (Exception e) {
			if (null != tx) {
				tx.rollback();
			}
		} finally {
			HibernateUtils.closeSession(session);
		}
	}

}
StudentService.java
package com.itlwc.service;

import com.itlwc.entity.Student;

public interface StudentService {
	public void saveStudent(Student stu);
}
StudentServiceImp.java
package com.itlwc.serviceImp;

import com.itlwc.dao.StudentDao;
import com.itlwc.daoImp.StudentDaoImp;
import com.itlwc.entity.Student;
import com.itlwc.service.StudentService;

public class StudentServiceImp implements StudentService {

	public void saveStudent(Student stu) {
		StudentDao studentDao = new StudentDaoImp();
		studentDao.saveStudent(stu);
	}

}
HibernateUtils.java
package com.itlwc.utils;

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

public class HibernateUtils {
	private static SessionFactory sessionFactory;
	static {
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}

	public static Session openSession() {
		Session session = sessionFactory.openSession();
		return session;
	}

	public static void closeSession(Session session) {
		if (null != session) {
			session.close();
		}
	}
}


你可能感兴趣的:(Hibernate3+Struts2简单案例)