IDEA 下写出第一个 SSH 整合框架练手项目(三.部门和员工的分页查询)
视频地址: 基于SSH实现员工管理系统之案例实现篇
我的整个完整源码地址 : 点击进入 github
这是SSH 整合项目的第三章,第一章基于Meavn 整合 Spring 和 Hibernate 框架并进行了进行了自动建表,第二章则在此基础上加入 Struts 2 框架以及完成一个简单的登陆页面。这章主要侧重于分页查询功能的实现
1.获取登陆用户的用户名并显示在页面头部,即(欢迎您:zs)
首先,我们已经在上一章将登陆用户的Employee 返回至Action,并且通过
ActionContext.getContext().getSession().put("existEmployee",existEmployee);
将通过用户输入的账号密码查询到的所有用户信息封装在 existEmployee 中并存入 session。
这时,只需要更改 top.html 为top.jsp,并将
<div class="div1">
欢迎您:张三
</div>
改为:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%--以上部分应该新增到 top.html 的顶部,普通的html标签省去不写--%>
<div class="div1">
欢迎您:<s:property value="#session.existEmployee.ename"/>
</div>
然后,运行,测试,成功修改。
2.实现分页查询用户的功能。
更改html文件为 jsp 文件。
更改成功后,将left.jsp更改,使点击
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="dtree.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="dtree.js"></script>
</head>
<body bgColor=#DDF0FB leftMargin=0 topMargin=0 marginwidth="0" marginheight="0">
<table width="90%" border="0" cellspacing="1" cellpadding="2" align="center">
<div class="dtree">
<script type="text/javascript">
d = new dTree('d');
d.add('01', '-1', '员工管理系统');
d.add('0101', '01', '人力资源部');
d.add('010101', '0101', '部门管理', 'listDep.html', '', 'right');
// 此处待修改
d.add('010102', '0101', '员工管理', 'employee_findAll.action', '', 'right');
// 此处更改是为了让点击 "员工管理" 后直接查询所有员工并打印在网页上。
document.write(d);
</script>
</div>
</table>
</body>
</html>
然后在 EmployeeAction 中进行查询操作,但应该现在服务器端分页,再传给用户端。
所以进行分页查询员工的操作。
首先在 domain 包下新建一个 PageBean.java 即每一页的 页面 对象
package com.test.ssh.domain;
import java.util.List;
/**
* 因为分页查询适用于员工和部门两个,所以使用多态<T>。
* @param <T>
*/
public class PageBean<T> {
private int currPage; //当前页面
private int pageSize; //每页显示的记录数
private int totalCount; //总记录数
private int totalPage; //总页数
private List<T> list; //没有显示的数据
//getter 和 setter 方法
}
然后在 Action 中定义 findAll 方法进行分页。
//首先,默认声明当前页数为 1
private Integer currPage = 1;
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
public String findAll() {
//使用 PageBean 对象装载分页情况
PageBean<Employee> pageBean = employeeService.fingByPage(currPage);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
EmployeeServiceImp.java 中定义 findAll(int currPage);
@Override
public PageBean<Employee> fingByPage(Integer currPage) {
PageBean<Employee> employeePageBean = new PageBean<Employee>();
// 封装当前的页数
employeePageBean.setCurrPage(currPage);
//封装每页显示的记录数,默认为3
int pageSize = 3;
employeePageBean.setPageSize(pageSize);
//封装总记录数,总记录数通过查询数据库获得
int totalCount = employeeDao.findCount();
employeePageBean.setTotalCount(totalCount);
//封装总页数
double tc = totalCount;
Double num = Math.ceil(tc / pageSize);
employeePageBean.setTotalPage(num.intValue());
//封装每页显示的数据
int begin = (currPage - 1) * pageSize;
List<Employee> list = employeeDao.findByPage(begin, pageSize);
employeePageBean.setList(list);
return employeePageBean;
}
EmployeeDaoImpl.java 中定义 findCount() 查询总员工数量,定义 findByPage(int begin,int pageSize); 查询当前页面的
@Override
public int findCount() {
//查询个数
String hql = "select count(*) from Employee ";
List<Long> list = this.getHibernateTemplate().find(hql);
//如果个数
if (list.size() > 0) {
return list.get(0).intValue();
//将Long 类型转为 int 类型后返回.
}
return 0;
}
@Override
public List<Employee> findByPage(int begin, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
List<Employee> list = this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);
return list;
}
此时,总页数都被定义好,同时当前页面需要显示的员工信息都已经被封装进 PageBean 。
在struts.xml 中新增一行,使action执行
<result name="findAll">/frame/listEmployee.jsp</result>
然后,将 pageBean 中的信息展示到页面中。并且使 首页 下一页 上一页 尾页
实现对应的功能
将 listEmployee.html 改为 listEmployee.jsp 后 更改代码为
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- ↑ 为 jsp 头文件 --%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title></title>
<style type="text/css">
.table1 {
border: 1px solid #ddd;
width: 900px;
}
thead {
background-color: lightblue;
}
</style>
</head>
<body>
<table border="0" width="900px">
<tr>
<td align="center" style="font-size:24px; color:#666"> 员工管理</td>
</tr>
<tr>
<%-- 此处暂时无用,但在后面的新增员工时起到作用,那时,不再修改 --%>
<td align="right"><a href="employee_saveUI.action">添加</a></td>
</tr>
</table>
<br/>
<table cellspacing="0" border="1" class="table1">
<thead>
<tr>
<td align="center">编号</td>
<td align="center">员工姓名</td>
<td align="center">性别</td>
<td align="center">出生日期</td>
<td align="center">入职时间</td>
<td align="center">所属部门</td>
<td align="center">编辑</td>
<td align="center">删除</td>
</tr>
</thead>
<tbody><s:iterator value="list" var="e">
<tr> <%-- 将原来的手动赋值改为从list 中得到的值 --%>
<td align="center"><s:property value="#e.eid"/></td>
<td align="center"><s:property value="#e.ename"/></td>
<td align="center"><s:property value="#e.sex"/></td>
<td align="center"><s:date name="#e.birthday" format="yyyy年MM月dd月"/></td>
<td align="center"><s:date name="#e.joinDate" format="yyyy年MM月dd月"/></td>
<td align="center"><s:property value="#e.department.dname"/></td>
<%-- 此处暂时无用,但在后面的修改员工和删除员工时将起到作用,那时,不再另改 --%>
<td align="center"><a href="employee_edit.action?eid=<s:property value="eid"/>"><img src="../images/编辑.png"></a>
</td>
<td align="center"><a href="employee_delete.action?eid=<s:property value="eid"/> "><img
src="../images/trash.gif"></a></td>
</tr>
</s:iterator></tbody>
</table>
<br/>
<table table border="0" cellspacing="0" cellpadding="0" width="900px">
<tr>
<td align="right"><span>第<s:property value="currPage"/> / <s:property value="totalPage"/> 页</span>
<span>总记录数:<s:property value="totalCount"/> 每页显示:<s:property value="pageSize"/>条</span> <s:if
test="currPage != 1"> <a href="employee_findAll.action?currPage=1">[首页]</a> <a
href="employee_findAll.action?currPage=<s:property value="currPage-1"/>">[上一页]</a> </s:if> <s:if
test="currPage!=totalPage"> <a
href="employee_findAll.action?currPage=<s:property value="currPage+1"/>">[下一页]</a> <a
href="employee_findAll.action?currPage=<s:property value="totalPage"/> ">[尾页]</a> </s:if> </span>
</td>
</tr>
</table>
</body>
</html>
然后执行,看到zs 一个用户,在数据库中多加几个Employee后再次测试。
进行测试。
如果成功,则分页查询成功.
3.实现部门的分页查询.
刚刚实现了员工的分页查询,现在来实现部门的分页查询,与Employee 的一样。
首先新建DepartmentDao,DepartmentDaoImpl, DepartmentService,DepartmentServiceImpl .
位置如图(与Employee对应的类相同包)
首先更改left.jsp
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="dtree.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="dtree.js"></script>
</head>
<body bgColor=#DDF0FB leftMargin=0 topMargin=0 marginwidth="0" marginheight="0">
<table width="90%" border="0" cellspacing="1" cellpadding="2" align="center">
<div class="dtree">
<script type="text/javascript">
d = new dTree('d');
d.add('01', '-1', '员工管理系统');
d.add('0101', '01', '人力资源部');
d.add('010101', '0101', '部门管理', 'department_findAll.action', '', 'right');
// 此处更改为 查询所有的 action 操作,理由与下相同
d.add('010102', '0101', '员工管理', 'employee_findAll.action', '', 'right');
// 此处更改是为了让点击 "员工管理" 后直接查询所有员工并打印在网页上。
document.write(d);
</script>
</div>
</table>
</body>
</html>
在DepartmentAction.java里面写出 分页查询。与EmployeeAction 中的findAll () 相同。
package com.test.ssh.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.test.ssh.domain.Department;
import com.test.ssh.domain.PageBean;
import com.test.ssh.service.DepartmentService;
import com.test.ssh.service.impl.DepartmentServiceImpl;
public class DepartmentAction extends ActionSupport implements ModelDriven<Department> {
//模型使用的驱动
private Department department = new Department();
private DepartmentService departmentService;
/**
* 查询并设置页数的操作
* 先默认当前页面 currPage = 1
*/
private Integer currPage = 1;
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
public String findAll() {
PageBean<Department> pageBean = departmentService.findByPage(currPage);
//将pageBean 存入到值栈中
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
public void setDepartment(Department department) {
this.department = department;
}
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
@Override
public Department getModel() {
return department;
}
}
DepartmentServiceImpl.java
package com.test.ssh.service.impl;
import com.test.ssh.dao.DepartmentDao;
import com.test.ssh.domain.Department;
import com.test.ssh.domain.Employee;
import com.test.ssh.domain.PageBean;
import com.test.ssh.service.DepartmentService;
import java.util.List;
public class DepartmentServiceImpl implements DepartmentService {
private DepartmentDao departmentDao;
public void setDepartmentDao(DepartmentDao departmentDao) {
this.departmentDao = departmentDao;
}
@Override
public PageBean<Department> findByPage(Integer currPage) {
PageBean<Department> departmentPageBean = new PageBean<Department>();
// 封装当前的页数
departmentPageBean.setCurrPage(currPage);
//封装每页显示的记录数,默认为3
int pageSize = 3;
departmentPageBean.setPageSize(pageSize);
//封装总记录数,总记录数通过查询数据库获得
int totalCount = departmentDao.findCount();
departmentPageBean.setTotalCount(totalCount);
//封装总页数
double tc = totalCount;
Double num = Math.ceil(tc / pageSize);
departmentPageBean.setTotalPage(num.intValue());
//封装每页显示的数据
int begin = (currPage - 1) * pageSize;
List<Department> list = departmentDao.findByPage(begin, pageSize);
departmentPageBean.setList(list);
return departmentPageBean;
}
}
DepartmentDaoImpl.java
package com.test.ssh.dao.impl;
import com.test.ssh.dao.DepartmentDao;
import com.test.ssh.dao.EmployeeDao;
import com.test.ssh.domain.Department;
import com.test.ssh.domain.Employee;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;
public class DepartmentDaoImpl extends HibernateDaoSupport implements DepartmentDao {
@Override
public int findCount() {
//查询个数
String hql = "select count(*) from Department ";
List<Long> list = this.getHibernateTemplate().find(hql);
//如果个数
if (list.size() > 0) {
return list.get(0).intValue();
//将Long 类型转为 int 类型后返回.
}
return 0;
}
@Override
public List<Department> findByPage(int begin, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
List<Department> list = this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);
return list;
}
}
然后在applictionContext.xml中配置自动注入
<bean id="departmentAction" class="com.test.ssh.action.DepartmentAction" scope="prototype">
<property name="departmentService" ref="departmentService"/>
</bean>
<bean id="departmentService" class="com.test.ssh.service.impl.DepartmentServiceImpl">
<property name="departmentDao" ref="departmentDao"/>
</bean>
<bean id="departmentDao" class="com.test.ssh.dao.impl.DepartmentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
然后更改Struts.xml,使Department_*.action 可以被拦截到DepartmentAction中
<action name="department_*" class="com.test.ssh.action.DepartmentAction" method="{1}">
<result name="findAll">/frame/listDep.jsp</result>
</action>
最后更改listDep.html为listDep.jsp
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.table1 {
border: 1px solid #ddd;
width: 900px;
}
thead {
background-color: lightblue;
}
</style>
</head>
<body>
<table border="0" width="900px">
<tr>
<td align="center" style="font-size:24px; color:#666"> 部门管理</td>
</tr>
<tr>
<td align="right"><a href="department_saveUI.action">添加</a></td>
<%--修改新增部门的 href,为后续做处理--%>
</tr>
</table>
<br/>
<table cellspacing="0" border="1" class="table1">
<tr>
<th width="450">部门名称</th>
<th width="450">编辑</th>
<th width="450">删除</th>
</tr>
<%--以下需要更改为动态请求--%>
<s:iterator value="list" var="d">
<tr>
<td align="center"><s:property value="#d.dname"/></td>
<td align="center"><a href="department_edit.action?did=<s:property value="#d.did"/>"><img
src="../images/编辑.png"></a></td>
<td align="center"><a href="department_delete.action?did=<s:property value="#d.did"/>"><img
src="../images/trash.gif"></a></td>
</tr>
</s:iterator>
</table>
<br/>
<table border="0" cellspacing="0" cellpadding="0" width="900px">
<tr>
<td align="right">
<%-- 这个与listEmployee的页面分页代码相同 --%>
<span>第<s:property value="currPage"/> / <s:property value="totalPage"/> 页</span>
<span>总记录数:<s:property value="totalCount"/> 每页显示:<s:property value="pageSize"/>条</span>
<span>
<s:if test="currPage != 1">
<a href="department_findAll.action?currPage=1">[首页]</a>
<a href="department_findAll.action?currPage=<s:property value="currPage-1"/>">[上一页]</a>
</s:if>
<s:if test="currPage!=totalPage">
<a href="department_findAll.action?currPage=<s:property value="currPage+1"/>">[下一页]</a>
<a href="department_findAll.action?currPage=<s:property value="totalPage"/> ">[尾页]</a>
</s:if>
</span>
</td>
</tr>
</table>
</body>
</html>
新增几行部门,测试。测试通过即成功。