package ht.util;
import java.util.*;
/**
* <p>Title: 通用分页程序</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* 使用方法
* ArrayList al = (ArrayList)session.getAttribute("sessionname");您的数据集列表
* AutoPage autopage = new AutoPage(al,5);
* if(request.getParameter("page")!=null){
* String tempStr = (String)request.getParameter("page");
* autopage.setCurrentPage((Integer.parseInt(tempStr)));
* }else{
* autopage.setCurrentPage(1);
* }
*
* autopage.setUrl(你的URL);
* ArrayList currentPageList = autoPage.getPageList();
* 输出currentPageList中内容
* String ctrlStr = autopage.getPageCtrlString();
* out.print(ctrlStr);
* 具体例子
* ArrayList alist = null;
if(session.getAttribute("planBillList")!=null)
{
alist = (ArrayList) session.getAttribute("planBillList");
}
//实例化自动分页类,第一个参数是要分页的结果列表(ArrayList或Vector),第二个参数是每页的行数
AutoPage autopage = new AutoPage(alist,3);
//设置显示结果的JSP页面
autopage.setUrl(GlobalsShare.APPNAME+"jsp/material/commissary/planbill_query_result.jsp");
//得到当前页
String strPage="1";
if(request.getParameter("page")!=null)
{
strPage = request.getParameter("page");
if(strPage==null)
{
strPage="1";
}
}
autopage.setCurrentPage(Integer.parseInt(strPage));
//得到当前页的结果集列表
ArrayList pageList = autopage.getPageList();
遁环pageList
//输出分页控制条
out.print(autopage.getPageCtrlString());
*
*/
public class AutoPage {
private int pageSize = 10;//每页大小
private int nextPage;//下一页
private int prePage;//前一页
private int pageCount;//总页数
private int currentPage;//当前页
private int listSize = 0;//记录总数
private ArrayList alist = new ArrayList();
private String url;
/**
* 初始化记录列表
* @param it Iterator
* @return ArrayList
*/
public ArrayList getAlist(Iterator it)
{
ArrayList al = new ArrayList();
while(it.hasNext())
{
al.add(it.next());
}
return al;
}
/**
* 构造方法
* @param list Collection
*/
public AutoPage(Collection list) {
alist = this.getAlist(list.iterator());
listSize = alist.size();
nextPage = 1;
prePage = 0;
pageCount = listSize/pageSize+1;
currentPage = 1;
}
/**
* 构造方法
* @param list Collection
* @param pageSize int
*/
public AutoPage(Collection list,int pageSize) {
alist = (ArrayList)list;
this.pageSize = pageSize;
listSize = alist.size();
nextPage = 1;
prePage = 0;
pageCount = listSize/pageSize;
if(listSize%pageSize>0)
{
pageCount = pageCount + 1;
}
currentPage = 1;
}
public AutoPage(Vector v,int pageSize)
{
for(int i = 0;i<v.size();i++)
{
alist.add(v.get(i));
}
this.pageSize = pageSize;
listSize = alist.size();
nextPage = 1;
prePage = 0;
pageCount = listSize/pageSize;
if(listSize%pageSize>0)
{
pageCount = pageCount + 1;
}
currentPage = 1;
}
/**
* 下一页
* @return ArrayList
*/
public int nextPage()
{
int tempInt = 1;
if(currentPage<this.getPageCount() && currentPage>=1)
{
tempInt =currentPage + 1;
}
else
tempInt = 1;
return tempInt;
}
/**
* 前一页
* @return ArrayList
*/
public int prePage()
{
int tempInt = 1;
if(currentPage>1)
{
tempInt = currentPage -1;
}else
tempInt = this.getPageCount();
return tempInt;
}
/**
* 第一页
* @return ArrayList
*/
public int firstPage()
{
nextPage = 1;
prePage = this.getPageCount();
currentPage = nextPage;
return currentPage;
}
/**
* 最后一页
* @return ArrayList
*/
public int endPage()
{
nextPage = this.getPageCount();
prePage = nextPage - 1;
currentPage = nextPage;
return currentPage;
}
/**
* 根据当前页得到记录列表
* @param currentPage int
* @return ArrayList
*/
public ArrayList getPageList()
{
ArrayList tempList = new ArrayList();
for(int i=(currentPage-1)*pageSize;i<(currentPage-1)*pageSize+pageSize;i++)
{
if(i>=0 && i<this.alist.size())
{
tempList.add(alist.get(i));
}else
break;
}
return tempList;
}
public String getPageCtrlString()
{
String connector="?";
if(this.getUrl().indexOf("?")>0){
connector="&";
}
String strCtrl = "";
if(this.currentPage == 1){
strCtrl = "【第一页】 ";
}else{
strCtrl = "【<a href='" + url + connector+"page=1'>第一页</a>】 ";
}
if(this.currentPage == 1){
strCtrl = strCtrl + "【上一页】 ";
}else{
strCtrl = strCtrl + "【<a href='" + url + connector+"page=" + this.prePage() + "'>上一页</a>】 ";
}
if(this.currentPage == this.pageCount){
strCtrl = strCtrl + "【下一页】 ";
}else{
strCtrl = strCtrl + "【<a href='" + url + connector+"page=" + this.nextPage() + "'>下一页</a>】 ";
}
if(this.currentPage == this.pageCount){
strCtrl = strCtrl + "【最后页】 ";
}else{
strCtrl = strCtrl + "【<a href='" + url +connector+ "page=" + this.getPageCount() + "'>最后页</a>】 ";
}
strCtrl = strCtrl + "跳到 <select name='toPage' onChange=/"window.location='" + url + connector+"page='+this.options[this.selectedIndex].value;/">";
for(int i=1;i<=this.getPageCount();i++)
{
if(i==this.getCurrentPage())
{
strCtrl = strCtrl + "<option value='"+i+"' selected>第"+i+"页</option>";
}else
{
strCtrl = strCtrl + "<option value='"+i+"'>第"+i+"页</option>";
}
}
strCtrl = strCtrl + "</select>";
strCtrl = strCtrl + " 本页 第 " + this.getCurrentPage() + " 页 每页 " + this.getPageSize() + " 条 共 " + this.listSize + " 条 共 " + this.getPageCount() + " 页";
//return strCtrl;
return "<div style='zIndex:1'>" + strCtrl + "<div>";
}
public int getCurrentPage() {
return currentPage;
}
public int getNextPage() {
return nextPage;
}
public int getPageCount() {
return pageCount;
}
public int getPageSize() {
return pageSize;
}
public int getPrePage() {
return prePage;
}
public String getUrl() {
return url;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public void setCurrentPage(int currentPage) {
if(currentPage>this.getPageCount())
{
this.currentPage=this.getPageCount();
}else if(currentPage<0)
{
this.currentPage=1;
}else
{
this.currentPage = currentPage;
}
}
public void setUrl(String url) {
this.url = url;
}
}
Jsp中使用该分页程序代码片段--
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@include file="/inc/checklogin.jsp"%>
<%@page import="ht.util.AutoPage"%>
<%@ page import="java.util.*,java.text.*" %>
<html>
<head>
<title>操作票登记维护</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<link href="<%=request.getContextPath()%>/css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<%=request.getContextPath()%>/js/ajax.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
function add(){
window.location="/scxt/opeRecordsAction.do?method=beforeAdd";
}
function edit(){
var checkFlag = false;
var recordId = "";
var selectFlagArray= document.getElementsByName("selectFlag");
if(selectFlagArray==undefined){
alert("没有要编辑的记录!");
return false;
}
//alert(document.all.selectFlag.length);
for(var i=0;i<selectFlagArray.length;i++){
if(selectFlagArray[i].checked){
recordId = selectFlagArray[i].value;
checkFlag = true;
}
}
if(!checkFlag){
alert("请选择要编辑的记录!");
return false;
}else{
location.href="<%=request.getContextPath()%>/opeRecordsAction.do?method=beforeEdit&recordId="+recordId;
}
}
function del(){
var checkFlag = false;
var recordId = "";
var selectFlagArray= document.getElementsByName("selectFlag");
if(selectFlagArray==undefined){
alert("没有要删除的记录!");
return false;
}
for(var i=0;i<selectFlagArray.length;i++){
if(selectFlagArray[i].checked){
recordId = selectFlagArray[i].value;
checkFlag = true;
}
}
if(!checkFlag){
alert("请选择要删除的记录!");
return false;
}else{
if(confirm("确定要删除此记录?")){
location.href="<%=request.getContextPath()%>/opeRecordsAction.do?method=del&recordId="+recordId;
}else{
return false;
}
}
}
function view(){
var checkFlag = false;
var recordId = "";
var selectFlagArray= document.getElementsByName("selectFlag");
if(selectFlagArray==undefined){
alert("没有要查看详情的记录!");
return false;
}
for(var i=0;i<selectFlagArray.length;i++){
if(selectFlagArray[i].checked){
recordId = selectFlagArray[i].value;
checkFlag = true;
}
}
if(!checkFlag){
alert("请选择要查看详情的记录!");
return false;
}else{
location.href="<%=request.getContextPath()%>/opeRecordsAction.do?method=view&recordId="+recordId;
}
}
</script>
</head>
<body>
<%@include file="/inc/myconfirm.jsp"%>
<%
Date dd=new Date();
String pattern="yyyy-MM-dd HH:mm:ss";
//当前时间
SimpleDateFormat currentTime=new SimpleDateFormat(pattern);
String currentTimeStr=currentTime.format(dd);
//初始化查询条件
List hOpFacL = (List)session.getAttribute("hOpFacL");//运行单位列表
List dutyList = (List)session.getAttribute("dutyList");//班次列表
List spciltyList = (List)session.getAttribute("spciltyList");//专业列表
List zbList = (List)session.getAttribute("zbList");//值别列表
%>
<TABLE width="100%" border=0 align="center" cellPadding=3 cellSpacing=0>
<TBODY>
<TR>
<TD align="center">
<SPAN class=bigfont>操作票登记维护</SPAN>
</TD>
</TR>
</TBODY>
</TABLE>
<div align="center" id="div1">
<form action="<%=request.getContextPath()%>/opeRecordsAction.do?method=query" name="form1" method="post">
<table width="100%" border="1">
<tr>
<td>
运行单位
</td>
<td>
<select name="factoryId">
<option value="">请选择</option>
<%if(hOpFacL.size()!= 0 ){
for(int j = 0;j<hOpFacL.size();j++){
List tempList = (List)hOpFacL.get(j);
%>
<option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
<%
}
}
%>
</select>
</td>
<td>
专业
</td>
<td>
<select name="spciltyId">
<option value="">请选择</option>
<%if(spciltyList.size()!= 0 ){
for(int j = 0;j<spciltyList.size();j++){
List tempList = (List)spciltyList.get(j);
%>
<option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
<%
}
}
%>
</select>
</td>
</tr>
<tr>
<td>
班次
</td>
<td>
<select name="dutyId">
<option value="">请选择</option>
<%if(dutyList.size()!= 0 ){
for(int j = 0;j<dutyList.size();j++){
List tempList = (List)dutyList.get(j);
%>
<option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
<%
}
}
%>
</select>
</td>
<td>
值别
</td>
<td>
<select name="classId">
<option value="">请选择</option>
<%if(zbList.size()!= 0 ){
for(int j = 0;j<zbList.size();j++){
List tempList = (List)zbList.get(j);
%>
<option value="<%=(String)tempList.get(0)%>"><%=(String)tempList.get(1)%></option>
<%
}
}
%>
</select>
</td>
<tr>
<td >
接令时间
</td>
<td colspan="3">
<input type="text" name="startTime" size="30" readonly οnclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<%=currentTimeStr%>">
至
<input type="text" name="endTime" size="30" readonly οnclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<%=currentTimeStr%>">
</td>
</tr>
<tr>
<td colspan="4" align="center">
<input type="submit" id="buttonSubmit" value="查询" >
</td>
</tr>
</table>
</form>
</div>
<hr>
<TABLE width="100%" border=0 align="center" cellPadding=3 cellSpacing=0>
<TBODY>
<TR>
<TD height="41" align="center">
<SPAN class=bigfont>查询结果</SPAN>
</TD>
</TR>
</TBODY>
</TABLE>
<logic:notEmpty name="opeRecordsList" scope="session">
<%
ArrayList alist = null;
if (session.getAttribute("opeRecordsList") != null) {
alist = (ArrayList) session.getAttribute("opeRecordsList");
}
AutoPage autopage = new AutoPage(alist, 10);
autopage.setUrl(request.getContextPath()
+ "/jsp/rundata/operecords/opeRecordsManage.jsp");
String strPage = "1";
if (request.getParameter("page") != null) {
strPage = request.getParameter("page");
if (strPage == null) {
strPage = "1";
}
}
autopage.setCurrentPage(Integer.parseInt(strPage));
ArrayList pageList = autopage.getPageList();
request.setAttribute("pageList", pageList);
%>
<TABLE width="100%" border="0" align="center" cellpadding="1"
cellspacing="1" bgcolor="#666666" id="table_dirttype">
<TBODY>
<THEAD>
<TR class=TableHeader>
<td width="11%" height="29" align="center" nowrap class=tablehead>
选择
</Td>
<td width="11%" height="29" align="center" nowrap class=tablehead>
接票时间
</Td>
<td width="9%" align="center" nowrap class=tablehead>
操作票
</Td>
<td width="9%" align="center" nowrap class=tablehead>
操作票编号
</Td>
<td width="11%" height="29" align="center" class=tablehead>
原因
</Td>
</TR>
</THEAD>
<logic:iterate id="opeRecord" name="pageList" scope="request">
<TR bgcolor="#ffffff"
onMouseOver="this.style.backgroundColor='#fee5ca'"
onMouseOut="this.style.backgroundColor='#ffffff'"
id="tr_<bean:write name='opeRecord' property='opeid'></bean:write>">
<TD align=center noWrap>
<input type="radio" id="selectFlag" name="selectFlag" value="<bean:write name='opeRecord' property='opeid'></bean:write>">
</TD>
<TD align=center noWrap>
<bean:write name='opeRecord' property='gettime'></bean:write>
</TD>
<TD align=center noWrap>
<c:forEach var="opeModel" items="${OpeModelList}">
<c:if test="${opeModel.modelid==opeRecord.modelid}">
${opeModel.modelname}
</c:if>
</c:forEach>
</TD>
<TD align=center noWrap>
<bean:write name='opeRecord' property='openum'></bean:write>
</TD>
<TD align=center noWrap>
<bean:write name='opeRecord' property='reason'></bean:write>
</TD>
</TR>
</logic:iterate>
</TABLE>
<table width="95%" border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td height="25" align="center">
<%
out.print(autopage.getPageCtrlString());
%>
</td>
</tr>
</table>
</logic:notEmpty>
<logic:empty name="pageList" scope="request">
<br>
<br>
<br>
<div align="center" title="提示信息框">
<span style="BACKGROUND:#fff;COLOR:#666;margin: 10px;border:1px dotted #666;font-weight:bold;padding:8px;width=200">
无记录!
</span>
</div>
<br>
<br>
</logic:empty>
<hr>
<div align="center">
<input type="button" id="addButton" value="新增" οnclick="javaScript:add();" class="fm">
<input type="button" id="editButton" value="编辑" οnclick="javaScript:edit();" class="fm">
<input type="button" id="delButton" value="删除" οnclick="javaScript:del();" class="fm">
<input type="button" id="viewButton" value="查看详情" οnclick="view();" class="fm">
</div>
<script type="text/javascript">
var opeFlag=${opeFlag};
var firstEntryFlag =${firstEntryFlag};
if(opeFlag.toString()=='false'){
if(firstEntryFlag.toString()=='true'){
alert("您不是当班人员,仅能进行查询操作!");
}
document.getElementById("addButton").disabled="true";
document.getElementById("delButton").disabled="true";
document.getElementById("editButton").disabled="true";
}
</script>
</body>
</html>