分页
真分页
使用特定的sql语句,条件查询出指定内容假分页
数据全部取出,在页面分页显示分页数据
pageSize maxResults
每页大小 页面(用户提供或默认值)提供
itemCount
总记录数 数据库
offset firstResult
当前页第一条的编号 当前显示第几页(用户) 计算
pageCount
总页数 计算
总页数 = (总记录数+每页大小 - 1 )/ 每页大小
当前页第一条的编号 = (当前页编号 - 1) * 每页大小
- 分页完成步骤
hibernate能分页
拷贝PageInfo daoimp增加byPage方法 byPage和getAll方法的区别在与要设置max和first,要查询总数页面输出能分页
action调用分页查询的方法,由于分页参数是页面提供,所以pageInfo 设置为action的成员变量,getter setter
list页面调用分页的方法
浏览器地址 输入list.jsp?pageInfo.firstResult=15&pageInfo.maxResults=5使用自定义分页标签能分页
拷贝分页标签类 和 分页标签的tld文件 tld文件放在web-inf 检查tld文件中类的路径名称是否匹配
list页面 添加自定义标签的引入
<%@ taglib prefix="paging" uri="/WEB-INF/paging.tld" %>
记得设置base标签
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<s:action name="courseAction_getAllCourse" executeResult="false"
namespace="/" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>" />
拷贝标签的调用
<tr>
<td colspan="8">
<paging:paging
href="student/list.jsp"
itemCount="{pageInfo.firstResult}"
maxResults="${pageInfo.maxResults}"/>
</td></tr>
PageInfo.java:
package repairsystem.page;
import java.io.Serializable;
public class PageInfo implements Serializable {
private static final long serialVersionUID = 1878154374135400744L;
private int firstResult = 0;//当前页第一条编号
private int maxResults = 5;//每页大小
private int pageCount = 0;//总页数
private transient int itemCount = 0;//总记录数
public boolean isPage = false;
public int getFirstResult() {
return firstResult;
}
public void setFirstResult(int firstResult) {
this.firstResult = firstResult;
}
public int getMaxResults() {
return maxResults;
}
public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public boolean isPage() {
return isPage;
}
public void setPage(boolean isPage) {
this.isPage = isPage;
}
public int getItemCount() {
return itemCount;
}
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
this.pageCount = (itemCount + maxResults - 1) / maxResults;
}
}
PagingTag.java:
package repairsystem.page;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author vikings
*
*/
public class PagingTag extends TagSupport{
private static final long serialVersionUID = 1L;
private int maxResults = 5;
private int firstResult=0;
private int itemCount = 0;
private String href = null;
public int getMaxResults() {
return maxResults;
}
public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
public int getFirstResult() {
return firstResult;
}
public void setFirstResult(int firstResult) {
this.firstResult = firstResult;
}
public int getItemCount() {
return itemCount;
}
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public int doStartTag() throws JspException
{
return SKIP_BODY;
}
public int doEndTag() throws JspException
{
int totalPages = 0;
int currentPage=0;
totalPages = itemCount / maxResults;
if(itemCount==0||maxResults==0){
return EVAL_PAGE;
}
if (itemCount % maxResults != 0) {
totalPages = totalPages + 1;
}
if (totalPages<=1)
{
return EVAL_PAGE;
}
currentPage = (firstResult) / maxResults;
currentPage = currentPage+1;
StringBuffer navigation = new StringBuffer();
navigation.append("<li><a>第"+currentPage+"页/共"+totalPages+"页</a></li>");
if (currentPage<=1){
navigation.append("<li><a>首页</a></li>");
navigation.append("<li><a>上一页</a></li>");
} else {
navigation.append("<li><a href='"+href+"?pageInfo.firstResult=0&pageInfo.maxResults="+maxResults+"&pageInfo.isPage=true'>首页</a></li>");
navigation.append(" ");
navigation.append("<li><a href='"+href+"?pageInfo.firstResult="+(firstResult-maxResults)+"&pageInfo.maxResults="+maxResults+"&pageInfo.isPage=true'>上一页</a></li>");
}
navigation.append(" ");
if (currentPage>=totalPages){
navigation.append("<li><a>下一页</a></li>");
navigation.append(" ");
navigation.append("<li><a>尾页</a></li>");
}else {
navigation.append("<li><a href='"+href+"?pageInfo.firstResult="+(firstResult+maxResults)+"&pageInfo.maxResults="+maxResults+"&pageInfo.isPage=true'>下一页</a></li>");
navigation.append(" ");
navigation.append("<li><a href='"+href+"?pageInfo.firstResult="+((totalPages-1)*maxResults)+"&pageInfo.maxResults="+maxResults+"&pageInfo.isPage=true'>尾页 </a></li>");
}
try {
pageContext.getOut().print(navigation.toString());
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
public void release()
{
super.release();
this.maxResults = 0; //recordsPerPage
this.firstResult=0; //beginItem
this.itemCount = 0;
this.href = null;
}
}
paging.tld:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>Jsp Tag Library</shortname>
<!-- paging标签,用于显示一个分页的公告表格 -->
<tag>
<name>paging</name>
<tagclass>repairsystem.page.PagingTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>maxResults</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>firstResult</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>itemCount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>href</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
ServicemanDao.java:
// 分页查询
public List<Serviceman> getServicemanByPage(PageInfo pageInfo) {
Query query = session.createQuery("From Serviceman");
List<Serviceman> servicemans = null;
int itemCount = getItemCount();// 查询总记录数
pageInfo.setItemCount(itemCount);
// 总记录数大于0,才进行分页
if (itemCount > 0) {
query.setMaxResults(pageInfo.getMaxResults());
query.setFirstResult(pageInfo.getFirstResult());
servicemans = query.list();
}
// HibernateSessionFactory.closeSession();
return servicemans;
}
// 查询总记录数
public int getItemCount() {
Query query = session.createQuery("select count(*) From Serviceman");
int itemCount = Integer.parseInt(query.uniqueResult().toString());
// HibernateSessionFactory.closeSession();
return itemCount;
}
...
ServicemanAction.java:
public class ServicemanAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private Serviceman serviceman;
private ServicemanServiceI servicemanServiceI;
private PageInfo pageInfo;
public ServicemanAction() {
super();
servicemanServiceI = new ServicemanService();
pageInfo = new PageInfo();
}
public Serviceman getServiceman() {
return serviceman;
}
public void setServiceman(Serviceman serviceman) {
this.serviceman = serviceman;
}
public ServicemanServiceI getServicemanServiceI() {
return servicemanServiceI;
}
public void setServicemanServiceI(ServicemanServiceI servicemanServiceI) {
this.servicemanServiceI = servicemanServiceI;
}
public PageInfo getPageInfo() {
return pageInfo;
}
public void setPageInfo(PageInfo pageInfo) {
this.pageInfo = pageInfo;
}
// 得到所有维修人员信息
public String getServicemanInfo() throws Exception {
List<Serviceman> servicemans = servicemanServiceI.getServicemanInfo();
ServletActionContext.getRequest().setAttribute("servicemans", servicemans);
return null;
}
// 分页显示教师
public String getServicemanByPage() throws Exception {
// action依次调用service dao后得到结果,再原路返回结果到了action,再将结果转发给页面
System.out.println(pageInfo.getFirstResult() + " " + pageInfo.getMaxResults());
List<Serviceman> servicemans = servicemanServiceI.getServicemanByPage(pageInfo);
ServletActionContext.getRequest().setAttribute("servicemans", servicemans);
ServletActionContext.getRequest().setAttribute("pageInfo", pageInfo);
return null;
}
// 根据主键查询维修人员
public String getServicemanInfoBySid() throws Exception {
Serviceman dbserviceman = servicemanServiceI.getServicemanInfoBySid(serviceman.getSid());
ServletActionContext.getRequest().setAttribute("serviceman", dbserviceman);
// System.out.println(dbteacher.getTname());
return null;
}
// 增加维修人员
public String addServiceman() throws Exception {
Serializable result = servicemanServiceI.addServiceman(serviceman);
return null;
}
// 修改维修人员信息
public String updateServiceman() throws Exception {
Serializable result = servicemanServiceI.updateServiceman(serviceman);
return null;
}
}
serviceman.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="paging" uri="/WEB-INF/paging.tld" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<s:action name="ServicemanAction_getServicemanInfo" executeResult="false"
namespace="/" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>" />
<title>test</title>
</head>
<body>
<s:action name="ServicemanAction_getServicemanByPage" executeResult="false" namespace="/"/>
<table border="1">
<tr>
<td>维修者id</td>
<td>姓名</td>
<td>电话</td>
<td>密码</td>
<td>住址</td>
<td>状态</td>
<td colspan="4">操作</td>
</tr>
<s:iterator var="serviceman" value="#request.servicemans">
<tr>
<td><s:property value="#serviceman.sid" /></td>
<td><s:property value="#serviceman.sname" /></td>
<td><s:property value="#serviceman.sphone" /></td>
<td><s:property value="#serviceman.spassword" /></td>
<td><s:property value="#serviceman.saddress" /></td>
<td><s:property value="#serviceman.sstatus" /></td>
<td><a href="detail.jsp?sid=${serviceman.sid}">详情</a></td>
<td><a href="update.jsp?sid=${serviceman.sid}">修改</a></td>
<td>删除</td>
<td><a href="rob.jsp?sid=${serviceman.sid}">抢单</a></td>
</tr>
</s:iterator>
<tr>
<td><a href="add.jsp">增加</a></td>
</tr>
<tr>
<td colspan="8">
<paging:paging
href="Serviceman/serviceman.jsp"
itemCount="${pageInfo.itemCount}"
firstResult="${pageInfo.firstResult}"
maxResults="${pageInfo.maxResults}"/>
</td>
</tr>
</table>
</body>
</html>