package com.util;
import java.util.List;
/**
类名:PageData.java
创建人: zh
修改人:
完成功能:分页返回用
*/
public class PageData{
//列表
private List list;
//行数
private int count;
public PageData(){
}
public <T> PageData(int count,List<T> list)
{
this.count=count;
this.list=list;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
package com.util;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
public class PageUtil {
private int recordCount,pageSize,pageNo;//总数量 每页显示数量 页数
private int pageCount=0;//总页数
private String pageString,css="", url="";
/**
* 构造方法,初始化其他参数
* @param pageSize
* @param pageNo
* @param recordCount
* @param request
*/
public PageUtil(int pageSize,int pageNo,int recordCount,HttpServletRequest request)
{
this.pageNo=pageNo;
this.recordCount=recordCount;
if(this.recordCount<0)
this.recordCount=0;
this.pageSize=pageSize;
//链接地址
this.url=request.getRequestURI()+"?";
Enumeration pNames=request.getParameterNames();
while(pNames.hasMoreElements()){
String name=(String)pNames.nextElement();
if( name.equalsIgnoreCase("pageNo")==false )
{
if(url.charAt(url.length()-1)=='?')
url+=name+"="+request.getParameter(name);
else
url+="&"+name+"="+request.getParameter(name);
}
}
if(url.charAt(url.length()-1)!='?')
{
url+="&";
}
}
/**
* 计算拼接分页字符串
* @return
*/
public String getPageString()
{
if(recordCount%pageSize==0)
{
pageCount=recordCount/pageSize;
}
else
{
pageCount=recordCount/pageSize+1;
}
//当前页
if(pageNo<1)
{
pageNo=1;
}
//尾页
if(pageNo>pageCount)
{
pageNo=pageCount;
}
//上页和首页
if (pageNo>1)
{
pageString="<a href='https://www.ctyun.cn/portal/link.html?target=%22%2Burl%2B"pageNo=1'><font class='"+css+"'>首页</font></a> ";
pageString+="<a href='https://www.ctyun.cn/portal/link.html?target=%22%2Burl%2B"pageNo="+(pageNo-1)+"'><font class='"+css+"'>上页</font></a> ";
}
else
{
pageString="<font class='"+css+"'>首页</font> ";
pageString+="<font class='"+css+"'>上页</font> ";
}
pageString+="<font color=red>"+pageNo+"</font> ";
//下页和尾页
if(pageNo<pageCount)
{
pageString+="<a href='https://www.ctyun.cn/portal/link.html?target=%22%2Burl%2B"pageNo="+(pageNo+1)+"' class='"+css+"'>下页</a> ";
pageString+="<a href='https://www.ctyun.cn/portal/link.html?target=%22%2Burl%2B"pageNo="+pageCount+"' class='"+css+"'>尾页</a> ";
}
else
{
pageString+="<font class='"+css+"'>下页</font> ";
pageString+="<font class='"+css+"'>尾页</font>";
}
//跳转页
pageString+=" 转到第<select id=pageNo onchange=\"window.location.href='https://www.ctyun.cn/portal/link.html?target=%22%2Burl%2B"pageNo='+this.value\">";;
for(int i=1;i<=this.pageCount;i++) {
if( pageNo==i) {
pageString+="<option value="+i+" selected>"+i+"</option>";
} else {
pageString+="<option value="+i+">"+i+"</option>";
}
}
pageString+="</select>页";
return "<div align=center class="+css+">共"+pageCount+"页,"+recordCount+"条记录 "+pageString+"</div>";
}
public void setPageNo(int pageNo)
{
this.pageNo=pageNo;
}
public void setCss(String css)
{
this.css=css;
}
public void setUrl(String url)
{
this.url=url;
}
public static void main(String[] args) {
System.out.println("abc".charAt("abc".length()-1));
}
}
Enumeration pNames=request.getParameterNames();
while(pNames.hasMoreElements()){
String name=(String)pNames.nextElement();
if( name.equalsIgnoreCase("pageNo")==false )
{
if(url.charAt(url.length()-1)=='?')
url+=name+"="+request.getParameter(name);
else
url+="&"+name+"="+request.getParameter(name);
}
}
if(url.charAt(url.length()-1)!='?')
{
url+="&";
}
代码的作用详细说明如下:
如果去掉上面的一段枚举解析url的代码,点击下页会回到查询全部数据的情况。
正常带查询的坟茔应该是如下状态