参考别人的基础上,实现自己项目的分页
1.页面:page.jsp引入到其他列表页面中
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>分页内容</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
function first(){
document.getElementById("pageNow").value=1;
document.getElementById("selectedForm").submit();
}
function pre(){
var pageNow=document.getElementById("pageNow").value;
document.getElementById("pageNow").value=parseInt(pageNow)-1;
document.getElementById("selectedForm").submit();
}
function next(){
var pageNow=document.getElementById("pageNow").value;
document.getElementById("pageNow").value=parseInt(pageNow)+1;
document.getElementById("selectedForm").submit();
}
function last(){
var lastPage=document.getElementById("lastPage").value;
document.getElementById("pageNow").value=lastPage;
document.getElementById("selectedForm").submit();
}
</script>
</head>
<body>
<table>
<tr>
<td colspan="9">
共<s:property value="page.totalNum"/>条记录 共<s:property value="page.lastPage"/>页 第<s:property value="page.pageNow"/>页
<a href="javascript:first();">首页</a>
<a href="javascript:pre();">上一页</a>
<a href="javascript:next();">下一页</a>
<a href="javascript:last();">最后一页</a>
</td>
</tr>
</table>
</body>
</html>
2.在列表页面添加两个隐藏域:
<input id="pageNow" name="pageNow" type="hidden" value="<s:property value='pageNow'/>"/>
<input id="lastPage" type="hidden" value="<s:property value='page.lastPage'/>"/>
同时引入page.jsp:
<%@ include file="/common/paging.jsp" %>
3.Page实体类,当然不创建也行:
public class Page {
private int totalNum; //总记录数
private int lastPage; //最后一页页数
private int firstPage; //第一页
private int pageNow; //当前页数
private int pageSize; //一页包含多少条记录
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
4.action:
public String execute() throws Exception {
QueryOrder queryOrder = null;
if(isQuery == 1){
queryOrder = new QueryOrder();
if(queryWfsn != null){
queryOrder.setQueryWfsn(queryWfsn);
}
if(querySponsor != null){
queryOrder.setQuerySponsor(querySponsor);
}
if(queryServrity != null && !"0".equals(queryServrity)){
queryOrder.setQueryServrity(queryServrity);
}
if(queryWftitle != null){
queryOrder.setQueryWftitle(queryWftitle);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(startTime != null){
startTimeStr = sdf.format(startTime);
queryOrder.setStartTime(startTimeStr);
}
if(endTime != null){
endTimeStr = sdf.format(endTime);
queryOrder.setEndTime(endTimeStr);
}
}
page=OrderManager.getInstance().getListTotal(user.getUserId(), queryOrder, 1,"daiban");
if(pageNow<1){
pageNow=1;
}
//如果当前页数大于最大的页数,是当前页数永远是最后一页
if(pageNow>page.getLastPage()){
pageNow=page.getLastPage();
}
page.setPageNow(pageNow);
if(isQuery != 1){
queryOrder=null;
}
pendingOrderList = OrderManager.getInstance().queryPendingOrderList(user.getUserId(), queryOrder,page);
log.info(user.getUserName() + " 共有 " + pendingOrderList.size() + " 条待处理工单。");
return SUCCESS;
}
5.项目service和DAO层没有分开,所以处理逻辑和查询sql都在一个类中了
// 统计记录数和页数
public Page getListTotal(String userId, QueryOrder queryOrder,int curStaus,String menuName) {
int totalNum = 0;
ResultSet rs = null;
StringBuilder sql = new StringBuilder("select count(*) from cssf_order ");
Connection conn = ConnectionPool.getInstance().getConnection("cssf");
try {
log.info("查询工单列表的sql语句:"+sql.toString());
rs = conn.createStatement().executeQuery(sql.toString());
} catch (SQLException e1) {
log.error("查询工单列表异常:" + sql.toString());
e1.printStackTrace();
}
try {
while(rs.next()){
totalNum = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
Page page = new Page();
String pSize=prop.getProperty("pageSize");
if(pSize ==null || "".equals(pSize)){
pSize="10";
}
int pageSize = Integer.parseInt(pSize);
page.setPageSize(pageSize);
int lastPage = totalNum/pageSize;
if(totalNum%pageSize==0){
page.setLastPage(lastPage);
}
if(totalNum%pageSize!=0){
page.setLastPage(lastPage+1);
}
page.setTotalNum(totalNum);
return page;
}
方法queryPendingOrderList是查询列表的,很简单;其中page传两个参数:pageNow:当前第几页;pageSIze:每页多少行;
我用的数据库是oracle
分页的模式:
SELECT *
FROM (SELECT a.*, ROWNUM rn
FROM (SELECT *
FROM table_name) a
WHERE ROWNUM <= 40)
WHERE rn >= 21
最后的结果是: