action层
private Integer currPage=1;
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
/**
* 分页查询员工的执行的方法
* @return
*/
public String findAll() {
PageBean<Employee> pageBean = employeeService.findByPage(currPage);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
Service层
public PageBean<Employee> findByPage(Integer currPage) {
// TODO Auto-generated method stub
PageBean<Employee> pageBean = new PageBean<>();
// 封装当前的数据
pageBean.setCurrPage(currPage);
// 封装每页显示的记录数
int pageSize = 3;
pageBean.setPageSize(pageSize);
// 封装总记录数
int totalCount = employeeDao.findCount();
pageBean.setTotalCount(totalCount);
// 封装总页数
double tc = totalCount;
Double num = Math.ceil(tc/pageSize);
pageBean.setTotalPage(num.intValue());
// 封装每页显示的数据
int begin = (currPage - 1)* pageSize;
List<Employee> list = employeeDao.findByPage(begin,pageSize);
pageBean.setList(list);
return pageBean;
}
Dao层
@Override
public int findCount() {
String hql = "select count(*) from Employee";
List<Long> list = (List<Long>) this.getHibernateTemplate().find(hql);
if (list.size() > 0) {
return list.get(0).intValue();
}
return 0;
}
@Override
public List<Employee> findByPage(int begin, int pageSize) {
// TODO Auto-generated method stub
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
List<Employee> list = (List<Employee>) this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);
return list;
}
Domain
1.pageBean
package com.byf.ssh_employee.domain;
/**
* 分页封装的类
* @author hp
*
*/
import java.util.List;
public class PageBean<T> {
private int currPage; // 当前页数
private int pageSize; // 每页显示的记录数
private int totalCount; // 总记录数
private int totalPage; // 总页数
private List<T> list; // 每页显示的数据
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
2.Employee
package com.byf.ssh_employee.domain;
/**
* 员工的实体
* @author hp
*
*/
import java.util.Date;
public class Employee {
private Integer eid;
private String ename;
private String sex;
private Date birthday;
private Date joinDate;
private String eno;
private String username;
private String password;
// 所属的部门
private Department department;
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public String getEno() {
return eno;
}
public void setEno(String eno) {
this.eno = eno;
}
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 Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
Struts.xml
<action name="employee_*" class="employeeAction" method="{1}">
<result name="findAll">/jsp/employee/list.jsp</result>
</action>
请求查询
employee_findAll.action
显示查询的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<style type="text/css">
.table1 {
border: 1px solid #ddd;
width: 900px;
}
thead {
background-color: lightblue;
}
</style>
</head>
<body background="<%=basePath%>images/bg.jpg"
style="background-repeat: no-repeat; background-size: 100% 100%; background-attachment: fixed;">
<table border="0" width="900px">
<tr>
<td align="center" style="font-size: 24px; color: #666">部门管理</td>
</tr>
<tr>
<td align="right"><a
href="<%=basePath%>department_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>
</tr>
</thead>
<tbody>
<s:iterator value="list" var="e">
<tr>
<td align="center"><s:property value="#e.eno" /></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>
</tr>
</s:iterator>
</tbody>
</table>
<br />
<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> <span> <s:if test="currPage !=1">
<a href="<%=basePath%>department_findAll.action?currPage=1">[首页]</a>
<a
href="<%=basePath%>department_findAll.action?currPage=<s:property value="currPage-1" />">[上一页]</a>
</s:if> <s:if test="currPage !=totalPage">
<a
href="<%=basePath%>department_findAll.action?currPage=<s:property value="currPage+1" />">[下一页]</a>
<a
href="<%=basePath%>department_findAll.action?currPage=<s:property value="totalPage" />">[尾页]</a>
</s:if>
</span></td>
</tr>
</table>
</body>
</html>
关键:<s:date name="#e.birthday" format="yyyy-MM-dd"/>