您的位置:首页 > 数据库

数据库分页技术

2016-04-15 14:52 316 查看
news.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

<%@ page import="com.util.Page"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<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">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

<link type="text/css" rel="stylesheet" href="css/style.css" />

<script type="text/javascript" src="js/lhgcore.js"></script>

<script type="text/javascript" src="js/lhgcalendar.js"></script>

<link type="text/css" href="js/lhgcalendar.css" rel="stylesheet" />

</head>

<body>

<div id="top"></div>

<div

style="border: 1px solid #A60000; width:98%; float:left; margin: 5px 1%;"></div>

<div id="wrap">

<!--导航条开始-->

<div class="nav_demo">

<ul>

<a href="index.jsp"><li><font>首页</font></li></a>

<a href="news.jsp"><li><font>新闻</font></li></a>

<a href="communication.jsp"><li><font>交流合作</font></li></a>

<a href="pavilion.jsp"><li><font>展馆介绍</font></li></a>

<a href="forum.jsp"><li><font>论坛</font></li></a>

<a href="organization.jsp"><li><font>机构介绍</font></li></a>

</ul>

</div>

<!--导航条结束-->

<div class="mainbody">

<!--标题部分-->

<div class="bt">

<div class="bt1">

<font>新闻资讯</font>

</div>

<div class="bt2">

<div class="ar_m1" id="ar_m1">

<a href="index.jsp">中国发票博物馆</a>

</div>

<div class="ar_r1" id="ar_r1"></div>

<div class="ljr">

<div class="ar_l"></div>

<div class="ar_m2">

<span class="font1">新闻资讯</span>

</div>

<div class="ar_r2"></div>

</div>

</div>

</div>

<%

ArrayList list = (ArrayList) request.getAttribute("list");

//拿到当前页

String pageStr = request.getParameter("page");

int currentPage = 1;

if (pageStr != null)

currentPage = Integer.parseInt(pageStr);

//每页显示3条,共list.size()条数据,传进当前页面

Page pUtil = new Page(3, list.size(), currentPage);

//得到当前页

currentPage = pUtil.getCurrentPage();

if (list != null && list.size() != 0) {

for (int i = pUtil.getFromIndex(); i < pUtil.getToIndex(); i++) {

ArrayList al = (ArrayList) list.get(i);

%>

<form action="" method="get" id="forml" name="forml">

<div

style="border:1px solid #3cf; width:98%; float:left; margin-left:1%;"></div>

<!--新闻总览部分-->

<div class="newsarea">

<img class="newsimg" src="<%=al.get(5)%>" />

<div class="newsmain">

<div class="newstitle">

<a href="#" class="newstitlefont"><%=al.get(0)%></a>

</div>

<div class="newsdetail">

<%=al.get(6)%>…<a href="xwchakan.action?<%=al.get(4) %>>" class="detail">【详细】</a>

</div>

</div>

</div>

<%

}

}

%>

</form>

<!--总览部分结束-->

<div class="pages">

<font>

<tr>

<td width=100% bgcolor="#eeeeee" colspan=4 align="center">

记录总数:<%=pUtil.getRecordCount()%>条

当前页/总页数:<%=currentPage%> /<%=pUtil.getPageCount()%>

<!-- 每页显示:<%=pUtil.getPageSize()%>条-->

<a href="paihang.action?page=1">首页</a>

<a href="paihang.action?page=<%=(currentPage - 1)%>">上页</a>

<a href="paihang.action?page=<%=(currentPage + 1)%>">下页</a>

<a href="paihang.action?page=<%=pUtil.getPageCount()%>">末页</a>

</td>

</tr>

</font>

</div>

<!--预约开始-->

<!--预约结束-->

</div>

</div>

<!--底部footer开始-->

<div class="footer">

<font>河北经贸大学  发票博物馆</font> <font>版权所有-河北经贸大学移动电子商务实验室</font>

</div>

<!--底部footer结束-->

</body>

</html>

Page.java

package com.util;

public class Page {

private int pageSize;// 每页显示的条数

private int recordCount;// 总共的条数

private int currentPage;// 当前页面

public Page(int pageSize, int recordCount, int currentPage) {

this.pageSize = pageSize;

this.recordCount = recordCount;

setCurrentPage(currentPage);

}

// 构造方法

public Page(int pageSize, int recordCount) {

this(pageSize, recordCount, 1);

}

// 总页数

public int getPageCount() {

int size = recordCount / pageSize;// 总条数/每页显示的条数=总页数

int mod = recordCount % pageSize;// 最后一页的条数

if (mod != 0)

size++;

return recordCount == 0 ? 1 : size;

}

// 包含,起始索引为0

public int getFromIndex() {

// System.out.println("from index:"+(currentPage-1) * pageSize);

return (currentPage - 1) * pageSize;

}

// 不包含

public int getToIndex() {

// System.out.println("to index:"+Math.min(recordCount, currentPage *

// pageSize));

return Math.min(recordCount, currentPage * pageSize);

}

// 得到当前页

public int getCurrentPage() {

return currentPage;

}// 设置当前页

public void setCurrentPage(int currentPage) {

int validPage = currentPage <= 0 ? 1 : currentPage;

validPage = validPage > getPageCount() ? getPageCount() : validPage;

this.currentPage = validPage;

}// 得到每页显示的条数

public int getPageSize() {

return pageSize;

}// 设置每页显示的条数

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}// 得到总共的条数

public int getRecordCount() {

return recordCount;

}// 设置总共的条数

public void setRecordCount(int recordCount) {

this.recordCount = recordCount;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: