纯struts分页
网上写的都太专业,太复杂了(至少我看着复杂),分页无非就是取出特定的记录嘛
大致的分页思路如下:
1.取出分页处理中用到的各种变量,主要的几个有:记录集总数(rscount),到第几页(gopage),分页长度(pagelength),实际页码(realpage),总页数(pagecount)
2.根据上面的变量构造SQL语句,基本思路是取出 (当前页-1)*分页长度 开始的 分页长度 条记录
简单地写一个sql语句吧:select top 10 * from table where id not in (select top 20 id from table) 意思是取出第21条记录开始的10条记录.[使用not in的效率并不是很高,所以这种方法还有待改进,以后肯定不能这么取,现在么先用着]
自己根据项目需要写了个
先写了个pagedao.java类用来处理分页过程中的各种变量
package
net.caiban.dao;

import
java.sql.Connection;
import
java.sql.ResultSet;
import
java.sql.Statement;
import
java.util.ArrayList;

import
net.caiban.conn.DataConn;


public
class
PageDao
...
{

public int recordCount(String sql)...{ //取记录总数
DataConn dc = new DataConn();
Connection conn;

try ...{
conn = dc.getConnection();
Statement stmt = conn.createStatement();
//sql = "select count(t.*) as counts from product t "
// +"left join fenlei b on t.lbid=b.lbid "
// +"where b.isuse=0 and b.isdel=0 and t.isdel=0 and t.isuse=0 "
// +"order by adddate desc ";
ResultSet rs = stmt.executeQuery(sql);

if (rs.next()) ...{
return rs.getInt("counts");

} else ...{
return 1;
}

} catch (Exception e) ...{
System.out.println(e.getMessage());
}
return 0;
}

public int pageCount(int rscounts,int pagelength)...{ //取得总页数
int s;
int s1;
s=rscounts % pagelength;
s1=rscounts / pagelength;

if( s==0)...{
return s1;

}else...{
return s1+1;
}
}

public int getcurrpage(String page)...{ //取当前页 未做纯数字检查,函数我还不知道怎么写,改天补上

if ("".equals(page) || page==null )...{ //最好加上检测是否是数字的函数
return 1;

}else...{
return Integer.parseInt(page);
}
}

public int realPage(String gopage,String currpage,int pagecount)...{ //取出真实的页数
int a;
a=this.getcurrpage(currpage);
System.out.println("a:"+a); //调试用
System.out.println("gopage:"+gopage);
System.out.println("pagecount:"+pagecount);
System.out.println("===============realPage==================");

if (gopage==null || "".equals(gopage))...{ //第X页
return 1;
}

if("next".equals(gopage))...{ //下一页

if(a+1>=pagecount)...{
return pagecount;

}else...{
return a+1;
}
}

if("pre".equals(gopage))...{ //上一页

if(a-1<=1)...{
return 1;

}else...{
return a-1;
}
}

if("first".equals(gopage))...{ //最前页
return 1;
}

if("last".equals(gopage))...{ //最后页
return pagecount;
}

if(Integer.parseInt(gopage)>=pagecount)...{
return pagecount;

}else...{

if(Integer.parseInt(gopage)<= 1)...{
return 1;

}else...{
return Integer.parseInt(gopage);
}
}
}

public String setnav(int currpage,int pagecount)...{ //设置数字分页导航
String nav = "";
int f,l;

if(currpage+4>pagecount)...{
l=pagecount;

}else...{
l=currpage+4;
}

if(currpage-4<1)...{
f=1;

}else...{
f=currpage-4;
}

for(int i=f;i<=l;i++)...{

if(currpage==i)...{
nav+=" ["+i+"] ";

}else...{
nav+=" <a href="product.do?ac=tb&gopage="+i+"" >"+i+"</a> ";
}
}
return nav;
}

}
dao类里使用:
public
void
tbProduct(HttpServletRequest request)
...
{ //因为dao还要处理其他事务,所以这里只贴了个dao的函数,写得有点乱,还没时间去整理
PageDao pagedao=new PageDao();
String sql = null;
String gopage=request.getParameter("gopage"); //get page data from url
String currpage=request.getParameter("currpage"); //get current page from url
int pagelength=2; //每页显示的记录数
int rscounts; //记录总数
int pagecount; //总页数
int realpage; //实际页码
int tmpa;

sql = "select count (*) as counts from product t " //as counts 不能丢
+"left join fenlei b on t.lbid=b.lbid "
+"where b.isuse=0 and b.isdel=0 and t.isdel=0 and t.isuse=0 ";
System.out.println("sql1:"+sql);
rscounts=pagedao.recordCount(sql);
pagecount=pagedao.pageCount(rscounts, pagelength);
realpage=pagedao.realPage(gopage, currpage,pagecount);

if (realpage==0)...{
request.setAttribute("currpage", 1);
request.setAttribute("numList", pagedao.setnav(1, pagecount));
tmpa=1;

}else...{
request.setAttribute("currpage", realpage);
request.setAttribute("numList", pagedao.setnav(realpage, pagecount));
tmpa=pagelength * (realpage-1);
}

sql="select top "+ pagelength +" t.*,b.lbname from product t "
+"left join fenlei b on t.lbid=b.lbid "
+" where b.isuse=0 and b.isdel=0 and t.isdel=0 and t.isuse=0 "
+"and t.sysid not in (select top "+ tmpa +" t.sysid from product t "
+"left join fenlei b on t.lbid=b.lbid "
+" where b.isuse=0 and b.isdel=0 and t.isdel=0 and t.isuse=0 )";
System.out.println("sql2:"+sql);
DataConn dc = new DataConn();
Connection conn;
ArrayList products = new ArrayList();

try ...{
conn = dc.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int i = 0;

while (rs.next()) ...{
ProductForm proform= new ProductForm();
proform.setSysId(rs.getInt("sysid")); // get data
proform.setProductId(rs.getString("productId"));
proform.setProductName(rs.getString("productname"));
proform.setLbId(rs.getInt("lbid"));
proform.setBrand(rs.getString("brand"));
proform.setSpecs(rs.getString("specs"));
proform.setRefPrice(rs.getFloat("refprice"));
proform.setOrigin(rs.getString("origin"));
proform.setIntroduce(rs.getString("introduce"));
proform.setImgPath(rs.getString("imgpath"));
proform.setIsDel(rs.getInt("isdel"));
proform.setIsUse(rs.getInt("isuse"));
proform.setAddDate(rs.getDate("addDate"));
proform.setModifyDate(rs.getDate("modifyDate"));
proform.setLbName(rs.getString("lbname"));
products.add(i, proform);
// fenleis.add(fenlei);
i++;
}
request.setAttribute("tbProduct", products); //把取出来的数据放到tbProduct里,在jsp页面里可以用标签直接将里面内容打印出来.

} catch (Exception e) ...{
System.out.println(e.getMessage());
}
}
Action类里只要调用这个函数就可以了...
最后一个,在jsp页面里使用:
<
html:form
action
="/product"
method
="POST"
>
<!--
show fenlei list
-->
<
table
width
="700"
cellpadding
="3"
border
="1"
bgcolor
="#ffffff"
>
<
tr
>
<
th
>
产品编号
</
th
>
<
th
>
产品名称
</
th
>
<
th
>
产品分类
</
th
>
<
th
>
品牌
</
th
>
<
th
>
产品规格
</
th
>
<
th
>
添加时间
</
th
>
<
th
>
参考价格
</
th
>
<
th
>
操作
</
th
>
</
tr
>
<
logic:present
name
="tbProduct"
>
<
logic:iterate
name
="tbProduct"
id
="list"
>
<
tr
>
<
td
>
<
bean:write
name
="list"
property
="productId"
/></
td
>
<
td
>
<
A
style
="cursor:hand; color:#0000FF"
onclick
="javascript:window.open('product.do?ac=showit&sysid=<bean:write name="
list" property
="sysId"
/>
','','resizeable=no,menubar=no,scrollbars=yes,width=500,height=550,left=300,top=100');" >
<
bean:write
name
="list"
property
="productName"
/>
</
A
>
</
td
>
<
td
>
<
bean:write
name
="list"
property
="lbName"
/></
td
>
<
td
>
<
bean:write
name
="list"
property
="brand"
/></
td
>
<
td
>
<
bean:write
name
="list"
property
="specs"
/></
td
>
<
td
>
<
bean:write
name
="list"
property
="addDate"
/></
td
>
<
td
>
<
bean:write
name
="list"
property
="refPrice"
/></
td
>
<
td
>
<
span
style
="color:#0000ff; cursor:hand;"
onClick
="javascript:window.open('product.do?ac=showit&sysid=<bean:write name="
list" property
="sysId"
/>
','','resizeable=no,menubar=no,scrollbars=yes,width=500,height=550,left=300,top=100');">修改
</
span
>
|
<
a
href
="product.do?ac=delit&sysid=<bean:write name="
list" property
="sysId"
/>
" οnclick="return confirm('确定删除?');">删除
</
a
></
td
>
</
tr
>
</
logic:iterate
>
</
logic:present
>
</
table
>
<
a
href
="product.do?ac=tb&gopage=first"
>
首页
</
a
>
|
<
a
href
="product.do?ac=tb&gopage=pre&currpage=<logic:present name="
currpage"
><
bean:write
name
="currpage"
/></
logic:present
>
">上一页
</
a
>
<
logic:present
name
="numList"
>
${numList}
</
logic:present
>
<
a
href
="product.do?ac=tb&gopage=next&currpage=<logic:present name="
currpage"
><
bean:write
name
="currpage"
/></
logic:present
>
">下一页
</
a
>
|
<
a
href
="product.do?ac=tb&gopage=last"
>
末页
</
a
>
<!--
show fenlei list
-->
</
html:form
>
这里学了点新东西
< logic:present name ="numList" >
${numList} <!--这里原来用的<bean:write >bean:write打印出来的是html显示的代码,所以数字分页导航很难做,后面就干脆换成这种形式了,这种方法还是同事教的,HOHO-->
</ logic:present >
网上写的都太专业,太复杂了(至少我看着复杂),分页无非就是取出特定的记录嘛
大致的分页思路如下:
1.取出分页处理中用到的各种变量,主要的几个有:记录集总数(rscount),到第几页(gopage),分页长度(pagelength),实际页码(realpage),总页数(pagecount)
2.根据上面的变量构造SQL语句,基本思路是取出 (当前页-1)*分页长度 开始的 分页长度 条记录
简单地写一个sql语句吧:select top 10 * from table where id not in (select top 20 id from table) 意思是取出第21条记录开始的10条记录.[使用not in的效率并不是很高,所以这种方法还有待改进,以后肯定不能这么取,现在么先用着]
自己根据项目需要写了个
先写了个pagedao.java类用来处理分页过程中的各种变量

































































































































































dao类里使用:
















































































Action类里只要调用这个函数就可以了...
最后一个,在jsp页面里使用:













































这里学了点新东西
< logic:present name ="numList" >

</ logic:present >