比较笨的分页方法

Java:

sql += " order by id desc,addtime desc";
System.out.println("sql="+sql);
String tmp = this.getExpenseService().getCount("Expense", sql);

if(tmp==null){
tmp = "0";
}
String first = request.getParameter("first");
String up = request.getParameter("up");
String down = request.getParameter("down");
String last = request.getParameter("last");
String page = request.getParameter("page");

// int count = Integer.valueOf(tmp);
int count=Integer.parseInt(tmp);
int max = 0;
int pagePer=10;
if (count % pagePer != 0) {
max = count / pagePer + 1;
} else {
max = count / pagePer;
}
if (request.getParameter("page") == null) {
page = "1";
}
int page2 = Integer.parseInt(page) - 1;
if (down != null) {
page2 += 1;
}
if (up != null) {
page2 -= 1;
}
if (first != null) {
page2 = 0;
}
if (last != null) {
page2 = max - 1;
}
if (page2 > max - 1) {
page2 = max - 1;
}
if (page2 < 0) {
page2 = 0;
}
if (count < pagePer) {
page2 = 0;
}
List list = this.getExpenseService().getListByHQL("Expense",page2*pagePer,pagePer,sql);
System.out.println("list.size="+list.size());
request.setAttribute("page", page2 + 1 + "");
request.setAttribute("totel", count + "");
request.setAttribute("list", list);

html:

<%
String pager = (String) request.getAttribute("page");
String totel = (String) request.getAttribute("totel");
String total = (String) request.getAttribute("total");
if (pager == null) {
pager = "1";
}
if (totel == null) {
totel = "0";
}
if (total == null) {
total = "0";
}
%>
<table width="80%" border="0" cellpadding="0" cellspacing="0" bordercolor="#4DB0E7" align="center">
<tr bgcolor="#FFFFFF">
<td width="17%">总记录 <font color='red' ><%=totel%></font> 条
<input class="button_y" name="first" type="submit" value="首页">
<input class="button_y" name="up" type="submit" value="上一页">
<input class="button_y" name="down" type="submit" value="下一页">
<input class="button_y" name="last" type="submit" value="末页">
</td>
<td width="17%">当前页为第
<input style="text-align: center" type="text" size="2" name="page" value=<%=pager%> readonly="readonly" /> &nbsp页
</td>
</tr>
</table>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值