Struts2+Spring3+Hibernate3 v3.0

Struts2+Spring3+Hibernate3

 

版本信息:

1.对象模型主键采用自身唯一主键映射,如User中用username作为id

2.用Hibernate (JPA)注解进行映射

3.Spring用的注解作配置

4.Struts用的配置文件

5.页面用的Jquery框架

 

注:web部分仅实现了department模块,只是做了简单的实现

Department.java

package com.jvwl.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

/*
 * 系别信息
 */
@Entity
@Table(name="t_department")
public class Department {
	private String departmentId;
	private String name;
	private Set<Grade> grades;
	private int version;

	@Column(length=10)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Version
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@OneToMany(cascade={CascadeType.REMOVE},mappedBy="department")
	public Set<Grade> getGrades() {
		return grades;
	}

	public void setGrades(Set<Grade> grades) {
		this.grades = grades;
	}

	@Id @Column(length=20)
	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

}

  applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.jvwl.dao.impl,com.jvwl.service.impl,com.jvwl.action" />
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/student_info_system"/>
        <property name="username" value="root"/>
        <property name="password" value="jerval"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
			<list>
				<value>com.jvwl.model</value>
			</list>
		</property>
    </bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="allManagerMethod"
			expression="execution(* com.jvwl.service.impl.*.*(..))" />
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
	</aop:config>
</beans>

 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.objectFactory" value="spring" /> 
	<constant name="struts.ui.theme" value="jvwl" />
	<include file="struts_actions.xml" />

	<package name="jvwl" extends="json-default">
		<default-action-ref name="loginPage" />
		<global-results>
			<result name="login" type="redirect">/login.jsp</result>
			<result name="exception">/error.jsp</result>
		</global-results>
		<action name="loginPage">
			<result>/login.jsp</result>
		</action>
	</package>

</struts>

 struts_actions.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name="login" extends="jvwl" namespace="/">
		<action name="loginAction" class="loginAction">
			<result name="loginSuccess">/main.jsp</result>
			<result name="loginFail">/login.jsp</result>
		</action>
	</package>
	<package name="department" extends="jvwl" namespace="/department">
		<action name="departmentAction" class="departmentAction">
			<result name="add">/department/addDepartment.jsp</result>
			<result name="update">/department/updateDepartment.jsp</result>
			<result name="text" type="stream">
				<param name="contentType">text/html</param>
				<param name="inputName">inputStream</param>
			</result>
			<result name="pagination">/department/listDepartment.jsp</result>
			<result name="department">/department/updateDepartment.jsp</result>
		</action>
	</package>

</struts>

 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">
 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<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>
</web-app>

 DepartmentAction

package com.jvwl.action;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jvwl.bean.Pagination;
import com.jvwl.model.Department;
import com.jvwl.model.User;
import com.jvwl.service.DepartmentService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("departmentAction")
@Scope("prototype")
public class DepartmentAction extends ActionSupport implements
		ModelDriven<Department>, SessionAware {
	private static final long serialVersionUID = 4233464072288035445L;
	private DepartmentService departmentService;
	private Department department=new Department();
	private Map<String, Object> session;
	private ByteArrayInputStream inputStream;
	private int pageSize=20;
	private int pageNum=1;
	private Pagination<Department> pagination;

	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public String getOperateUserName() {
		return ((User) session.get("user")).getUsername();
	}

	public String add() {
		return "add";
	}

	public String addSave() {
		departmentService.addDepartment(department, getOperateUserName());
		inputStream = new ByteArrayInputStream("success添加".getBytes());
		return "text";
	}

	public String update() {
		department = departmentService.findDepartment(department.getDepartmentId());
		return "update";
	}

	public String updateSave() {
		departmentService.updateDepartment(department, getOperateUserName());
		inputStream = new ByteArrayInputStream("success修改".getBytes());
		return "text";
	}

	public String delete() {
		List<Serializable> ids = new ArrayList<Serializable>();
		ids.add(department.getDepartmentId());System.out.println(ids);
		departmentService.deleteDepartment(ids, getOperateUserName());
		findAll();
		return "pagination";
	}

	public String findOne() {
		department = departmentService.findDepartment(department.getDepartmentId());
		return "department";
	}

	public String findAll() {
		pagination = departmentService.findDepartments(null, pageNum, pageSize);
		return "pagination";
	}

	@Override
	public Department getModel() {
		return department;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Resource
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Department getDepartment() {
		return department;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public Pagination<Department> getPagination() {
		return pagination;
	}
}

 addDepartment.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加系别信息</title>
</head>
<body>
<form name="form1" method="post" action="departmentAction!addSave">
  <table width="600" border="0">
    <tr>
      <td align="right">系别编号</td>
      <td><input name="departmentId" type="text" id="departmentId" size="18"></td>
    </tr>
    <tr>
      <td align="right">系别名称 </td>
      <td><input type="text" name="name" id="name"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="提交"></td>
    </tr>
  </table>
<p>&nbsp;</p>
</form>
</body>
</html>

 updateDepartment.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>修改系别信息</title>
	</head>
	<body>
		<form name="form1" method="post" action="departmentAction!updateSave">
			<table width="600" border="0">
				<tr>
					<td align="right">
						系别编号
					</td>
					<td><input id="version" name="version" type="hidden" value="<s:property value='department.version'/>">
						<input name="departmentId" type="text" id="departmentId" size="18"
							value="<s:property value='department.departmentId'/>">
					</td>
				</tr>
				<tr>
					<td align="right">
						系别名称
					</td>
					<td>
						<input type="text" name="name" id="name" value="<s:property value='department.name'/>">
					</td>
				</tr>
				<tr>
					<td>
						&nbsp;
					</td>
					<td>
						&nbsp;
					</td>
				</tr>
				<tr>
					<td>
						&nbsp;
					</td>
					<td>
						<input type="submit" name="button" id="button" value="提交">
					</td>
				</tr>
			</table>
			<p>
				&nbsp;
			</p>
		</form>
	</body>
</html>

 listDepartment.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title></title>
		<script type="text/javascript" src="../js/jquery-1.4.2.js"></script>
		<script type="text/javascript" src="../js/jquery.validate.js"></script>
		<script type="text/javascript" src="department.js"></script>
	</head>
	<body>
		<table width="100%">
			<thead>
				<tr>
					<th>
						&nbsp;
					</th>
					<th colspan="2">
						&nbsp;
					</th>
					<th>
						<a href="javascript:add()"><img alt="添加" src="../images/addoffer.png" /></a>
					</th>
				</tr>
				<tr>
					<th bgcolor="#C1E7FF">
						系别编号
					</th>
					<th colspan="2" bgcolor="#C1E7FF">
						系别名称
					</th>
					<th bgcolor="#C1E7FF">
						操作
					</th>
				</tr>
			</thead>
			<tbody id="dataArea">
				<s:iterator value="pagination.list" var="department">
					<tr>
						<s:set var="id" value="#department.departmentId" />
						<td>
							<s:property value="#id" />
						</td>
						<td colspan="2">
							<s:property value="#department.name" />
						</td>
						<td>
							<a href="javascript:update('<s:property value='#id'/>')">修改</a> |
							<a href="javascript:del('<s:property value='#id'/>')">删除</a>
						</td>
					</tr>
				</s:iterator>
				<tr>
					<td bgcolor="#C1E7FF">
						&nbsp;
					</td>
					<td colspan="2" bgcolor="#C1E7FF">
						&nbsp;
					</td>
					<td bgcolor="#C1E7FF">
						&nbsp;
					</td>
				</tr>
			</tbody>
			<tfoot id="footArea">
				<tr>
					<s:set var="pageNum" value="department.pageNum" />
					<td colspan="2" bgcolor="#C1E7FF">
						第
						<s:property value="#pageNum" />
						页,共
						<s:property value="department.maxPages" />
						页 |总记录数
						<s:property value="department.maxElements" />
						条
						<a href=""></a>
					</td>
					<td colspan="2" bgcolor="#C1E7FF">
						首页 |上一页 |下一页 |尾页
					</td>
				</tr>
			</tfoot>
		</table>
	</body>
</html>

 department.js

function add() {
	window.open("departmentAction!add", "aaaa", "width=400,height=400");
}

function update(id) {
	window.open("departmentAction!update?departmentId=" + id, "bbb", "width=400,height=400");
}

function del(id) {
	$.ajax( {
		type : "POST",
		url : "departmentAction!delete",
		cache : false,
		data : "departmentId=" + id,
		success : function(data) {
		alert("ok");
		}
	});
}

 

 

你可能感兴趣的:(hibernate3)