EmployeeDao 数据访问层的接口与实现
package cn.dao;
import java.util.List;
import cn.entity.Employee;
public interface EmployeeDao {
/**
* 查询所有员工
* @return
*/
List<Employee> findAll();
/**
* 添加员工
*/
String saveEmployee(Employee employee);
int deleteEmployee(String id);
List<Employee> findEmployees(String name);
/*
* 查询一共有多少个员工
*/
long findCount();
/*
* 查询一页数据
*/
List<Employee>findPage(int first , int max);
Employee getEmployee(String sn);
}
package cn.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.dao.EmployeeDao;
import cn.entity.Employee;
@SuppressWarnings("unchecked")
public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {
public List<Employee> findAll() {
return super.getHibernateTemplate().loadAll(Employee.class);
}
public String saveEmployee(Employee employee) {
return (String) super.getHibernateTemplate().save(employee);
}
public int deleteEmployee(String id) {
return super.getHibernateTemplate().bulkUpdate("delete from Employee e where e.sn=?",id);
}
public List<Employee> findEmployees( final String name) {
//使用Criytera Query
return super.getHibernateTemplate().executeFind(new HibernateCallback<List<Employee>>(){
//内部
public List<Employee>doInHibernate(Session session)throws HibernateException,
SQLException{
Criteria criteria=session.createCriteria(Employee.class);
criteria.add(Restrictions.like("name",name,MatchMode.ANYWHERE));
return criteria.list();
}
});
}
public long findCount() {
return super.getHibernateTemplate().execute(new HibernateCallback<Long>(){
public Long doInHibernate(Session session) throws HibernateException,
SQLException {
return (Long) session.createQuery("select count(*) from Employee").uniqueResult();
}
});
}
public List<Employee> findPage(int first, int max) {
DetachedCriteria criteria =DetachedCriteria.forClass(Employee.class);
criteria.addOrder(Order.desc("sn"));
return getHibernateTemplate().findByCriteria(criteria,first,max);
}
public Employee getEmployee(String sn) {
return super.getHibernateTemplate().get(Employee.class, sn);
}
}
EmployeeBiz 业务逻辑层接口与实现
package cn.biz;
import cn.entity.Employee;
public interface EmployeeBiz {
/**
* 登录
* @param sn
* @param password
* @return
*/
public Employee login(String sn,String password);
}
package cn.biz.impl;
import cn.dao.EmployeeDao;
import cn.entity.Employee;
import cn.biz.EmployeeBiz;
public class EmployeeBizImpl implements EmployeeBiz{
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
public Employee login(String sn, String password) {
Employee employee=employeeDao.getEmployee(sn);
if(employee!=null){
if(employee.getPassword().equals(password)){
return employee;
}
}
return null;
}
}
Consist 工具类
package cn.util;
public interface Consist {
/**
* 用户状态1-正常
*/
String EMP_NORMAL="1";
/**
* 用户状态0-离职
*/
String EMP_QUIT="0";
}
EmployeeAction 控制器
package cn.action;
import java.util.Map;
import cn.util.Consist;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cn.biz.EmployeeBiz;
import cn.entity.Employee;
/**
* 由 Spring 管理
* */
@SuppressWarnings("serial")
public class EmployeeAction extends ActionSupport{
private EmployeeBiz employeeBiz;
private Employee employee;
public void setEmployeeBiz(EmployeeBiz employeeBiz) {
this.employeeBiz = employeeBiz;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String login(){
Map<String,Object> session=ActionContext.getContext().getSession();
Employee emp=employeeBiz.login(employee.getSn(), employee.getPassword());
if(emp==null){
addActionError("用户名或密码不正确!");
}else{
if(Consist.EMP_NORMAL.equals(emp.getStatus())){
session.put("employee",emp);
return SUCCESS;
}else{
addActionError("该用户已经离职,登录失效!");
}
}
return INPUT;
}
}
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:oracle11" />
<property name="username" value="jboa" />
<property name="password" value="123456" />
</bean>
<!-- 会话工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/entity/Employee.hbm.xml</value>
<value>cn/entity/Department.hbm.xml</value>
<value>cn/entity/Position.hbm.xml</value>
</list>
</property>
</bean>
<!-- 在DAO中注入会话工厂 -->
<bean id="employeeDao" class="cn.dao.impl.EmployeeDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Employee业务类-->
<bean id="employeeBiz" class="cn.biz.impl.EmployeeBizImpl">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<!-- EmployeeAction类-->
<bean id="employeeAction" class="cn.action.EmployeeAction" scope="prototype">
<property name="employeeBiz" ref="employeeBiz"></property>
</bean>
</beans>
struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="cn.action" extends="struts-default" namespace="/">
<!-- 登录的 action -->
<action name="login" class="employeeAction" method="login">
<result name="success">staff.jsp</result>
<result name="input">login.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">
<display-name></display-name>
<listener>
<description>Spring的配置</description>
<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>
<description>解决Hibernate延迟加载的配置</description>
<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>
<description>struts2的配置</description>
<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>
login.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>北大青鸟办公自动化管理系统</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
font: 12px 宋体;
background: #4BB8EF url(Images/bg.gif) repeat-x;
}
img {
border: 0;
}
.login-top {
width: 100%;
height: 186px;
margin: 147px auto 0;
background: url(Images/login_01.gif) no-repeat center 0;
}
.login-area {
width: 100%;
height: 140px;
margin: 0 auto;
background: url(Images/login_02.gif) no-repeat center 0;
}
.login-area form {
width: 290px;
margin: 0 auto;
}
.login-area label {
clear: left;
float: left;
margin-top: 13px;
width: 60px;
font: 600 14px 宋体;
}
.login-area input {
width: 122px;
height: 16px;
margin-top: 11px;
border: 1px #767F94 solid;
font: 12px/ 16px 宋体;
}
input.login-sub {
width: 204px;
height: 34px;
border: 0;
background: url(Images/login_sub.gif) no-repeat 90px 1px; *
margin-top: 5px;
}
.login-copyright {
width: 100%;
height: 30px;
margin: 18px auto 0;
background: url(Images/copyright.gif) no-repeat center 0;
}
</style>
</head>
<body>
<div class="login-top"></div>
<div class="login-area">
<form action="login.action" method="post">
<label>
工 号:
</label>
<input type="text" name="employee.sn" />
<label>
密 码:
</label>
<input type="password" name="employee.password" />
<input type="submit" class="login-sub" value="" />
<span style="color:red"><s:actionerror/></span>
</form>
</div>
<div class="login-copyright"></div>
</body>
</html>
效果图:
staff.jsp 页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>北大青鸟办公自动化管理系统</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body οnlοad="setCurTime()">
<s:if test="%{#session.employee!=null}">
<div class="top">
<div class="global-width">
<img src="Images/logo.gif" class="logo" />
</div>
</div>
<div class="status">
<div class="global-width">
<span class="usertype">【登录角色:${sessionScope.employee.department.name}<s:property value="#session.employee.position.nameCn"/>】</span>
${sessionScope.employee.name}你好!欢迎访问青鸟办公管理系统!
<a href="loginOut.action">注销</a>
</div>
</div>
<div class="main">
<div class="global-width">
<div class="nav" id="nav">
<div class="t"></div>
<dl class="open">
<dt
οnclick="this.parentNode.className=this.parentNode.className=='open'?'':'open';">
报销单管理
</dt>
<jsp:include page="menu.jsp"/>
</dl>
<dl>
<dt
οnclick="this.parentNode.className=this.parentNode.className=='open'?'':'open';">
我要采购
</dt>
<dd>
信心收件箱
</dd>
<dd>
信心发件箱
</dd>
</dl>
<dl>
<dt
οnclick="this.parentNode.className=this.parentNode.className=='open'?'':'open';">
我要销售
</dt>
<dd>
信心收件箱
</dd>
<dd>
信心发件箱
</dd>
</dl>
<dl>
<dt
οnclick="this.parentNode.className=this.parentNode.className=='open'?'':'open';">
信息中心
</dt>
<dd>
信心收件箱
</dd>
<dd>
信心发件箱
</dd>
</dl>
</div>
<form id="myForm" name="myForm" method="post">
<div class="action">
<div class="t">
增加报销单
</div>
<div class="pages">
<!--增加报销单 区域 开始-->
<table width="90%" border="0" cellspacing="0" cellpadding="0"
class="addform-base">
<caption>
基本信息
</caption>
<tr>
<td width="36%">
填写人:${employee.name}
<input type="hidden" name="claimVoucher.id"
value="${claimVoucher.id}" id="claimVoucherId" />
<input type="hidden" name="employee.sn" value="${employee.sn}" />
<input type="hidden" name="employee.name" value="${employee.name}" />
</td>
<td width="64%">
填报时间:
<span id="time"></span>
<input type="hidden" name="claimVoucher.createTime" id="createTime"
readonly="readonly" value="${claimVoucher.createTime}" />
</td>
</tr>
<tr>
<td>
总金额:${claimVoucher.totalAccount}
<input type="hidden" name="claimVoucher.totalAccount"
value="${claimVoucher.totalAccount}" />
</td>
<td>
状态:${claimVoucher.status}
<input type="hidden" id="claimVoucher.status"
name="claimVoucher.status" value="${claimVoucher.status}" />
</td>
</tr>
</table>
<p>
</p>
<table width="90%" border="0" cellspacing="0" cellpadding="0"
class="addform-item">
<thead>
<tr>
<td>
项目
</td>
<td>
金额
</td>
<td>
费用说明
</td>
<td>
操作
</td>
</tr>
</thead>
<c:if test="${details!=null}">
<c:forEach items="${details}" var="detail">
<tr>
<td>
<span id="item${detail.id}"> ${detail.item} </span>
</td>
<td>
<span id="account${detail.id}">¥${detail.account}</span>
</td>
<td>
<span id="desc${detail.id}">${detail.desc}</span>
</td>
<c:choose>
<c:when test="${claimVoucher.status!='新创建'}">
<td>
<img src="Images/edit.gif" width="16" height="16" title="编辑" />
</td>
</c:when>
<c:otherwise>
<td>
<a href="#" name="${detail.id}" οnclick="edit(this.name)"><img
src="Images/edit.gif" width="16" height="16" title="编辑" />
</a>
<a href="#" name="${detail.id}" οnclick="delDetail(this.name)"><img
src="Images/delete.gif" width="16" height="16" title="删除" /> </a>
</td>
</c:otherwise>
</c:choose>
</tr>
</c:forEach>
</c:if>
<tr>
<td>
<select name="claimVoucherDetail.item" id="claimVoucherDetailItem">
<option value="城际交通费">
城际交通费
</option>
<option value="市内交通费">
市内交通费
</option>
<option value="通讯费">
通讯费
</option>
<option value="礼品费">
礼品费
</option>
<option value="办公费">
办公费
</option>
<option value="交际餐费">
交际餐费
</option>
<option value="餐补">
餐补
</option>
<option value="住宿费">
住宿费
</option>
</select>
</td>
<td>
<input type="text" name="claimVoucherDetail.account"
id="claimVoucherDetailAccount" />
<input type="hidden" id="claimVoucherDetailId" />
</td>
<td>
<input type="text" name="claimVoucherDetail.desc"
id="claimVoucherDetailDesc" />
</td>
<c:choose>
<c:when test="${claimVoucher.status!='新创建'}">
<td>
<img src="Images/save.gif" width="16" height="16" title="保存此项" />
</td>
</c:when>
<c:otherwise>
<td>
<a href="javascript:addDetail();"> <img src="Images/save.gif"
width="16" height="16" title="保存此项" /> </a>
</td>
</c:otherwise>
</c:choose>
</tr>
<!--报销单事由-->
<tr>
<td colspan="4" class="event">
<label>
事 由:
</label>
<c:choose>
<c:when test="${claimVoucher.status!='新创建'}">
<textarea rows="5" cols="66" name="claimVoucher.event"
readonly="readonly">${claimVoucher.event}</textarea>
</c:when>
<c:otherwise>
<textarea rows="5" cols="66" name="claimVoucher.event" id="event">${claimVoucher.event}</textarea>
</c:otherwise>
</c:choose>
</td>
</tr>
<!--表单提交行-->
<tr>
<td colspan="4" class="submit">
<input type="button" id="1" name="1" value="保存" οnclick="save()" class="submit_01" />
<input type="button" id="2" name="2" value="提交" οnclick="submitAll()" class="submit_01" />
</td>
</tr>
</table>
<!--增加报销单 区域 结束-->
</div>
</div>
</form>
</div>
</div>
</s:if>
<s:else>
<h1>
你还没登录,请您先登录。
<a href="login.jsp">返回</a>
</h1>
</s:else>
<div class="copyright">
Copyright © 北大青鸟
</div>
</body>
</html>
效果图: