做分页的时候,我们通常都要显示如
『总共 50 页 现在是第 1 页/ 50 总页数 【1 2 3 4 5】 next 5 pages 上一页 下一页 』
这种页码头
大家是不是感觉设置起来页面jsp里面很乱,而且很容易出错啊
那现在就结合本人经验 下面是个通用的做法
首先 有个通用页面jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="com.synnex.erp.util.page.PageItems" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%
PageItems pi = (PageItems)request.getAttribute("PageItems");
int currentPage = Integer.parseInt(pi.getCurrentPage());
int recordSize = Integer.parseInt(pi.getRecordSize());
int size = Integer.parseInt(pi.getSize());
int pages = Integer.parseInt(pi.getPages());
%>
<script>
var form;
function setForm(form) {
this.form = form;
}
function pre1Page() {
var currentPage = new Number(form.currentPage.value)-1;
form.currentPage.value = currentPage;
form.action = window.location;
form.submit();
}
function pre5Pages() {
var currentPage = new Number(form.currentPage.value)-5;
form.currentPage.value = currentPage;
form.action = window.location;
form.submit();
}
function next1Page() {
var currentPage = new Number(form.currentPage.value)+1;
form.currentPage.value = currentPage;
form.action = window.location;
form.submit();
}
function next5Pages() {
var currentPage = new Number(form.currentPage.value)+5;
form.currentPage.value = currentPage;
form.action = window.location;
form.submit();
}
function toPage(pageNo) {
var currentPage = pageNo;
form.currentPage.value = currentPage;
form.action = window.location;
form.submit();
}
</script>
<html:hidden property="currentPage" styleId="currentPage" value="<%=pi.getCurrentPage()%>"/>
<logic:greaterThan name="PageItems" property="recordSize" value="0">
<table width="95%" border="0" cellpadding="3" cellspacing="2">
<tr class="MustField">
<td colspan="11">
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="sortTable">
<tr class="MustField">
<td width="150" nowrap>
Total <%=pi.getRecordSize()%> Records
</td>
<td width="100" nowrap>
Page <%=pi.getCurrentPage()%> / <%=pi.getPages()%>
</td>
<td width="150" align="center" nowrap>
[
<%
int startPage;
if(currentPage%5==0) {
startPage = currentPage-5+1;
}else {
startPage = currentPage-(currentPage%5)+1;
}
int endPage = 0;
if(pages<5) {
endPage = pages;
}else {
endPage = startPage + 4;
}
if(pages<endPage) {
endPage = pages;
}
for(int i=startPage;i<=endPage;i++) {
String output = "";
if(i>pages) {
output = i+"";
out.println(output);
continue;
}
if(currentPage!=i) {
output = "<a href='javascript:toPage("+i+")'>"+i+"</a>";
}else {
output = i+"";
}
out.println(output);
}
%>
]
</td>
<td width="100" align="center" nowrap>
<%
if(currentPage-5<=0) {
}else {
out.println("<a href='javascript:pre5Pages()'>上 5 页</a>");
}
%>
</td>
<td width="100" align="center" nowrap>
<%
if(currentPage+5>=pages) {
}else {
out.println("<a href='javascript:next5Pages()'>下 5 页</a>");
}
%>
</td>
<td align="right" nowrap>
<%
if(currentPage>=pages) {
}else {
out.println("<a href='javascript:next1Page()'>下一页</a>");
}
if(currentPage<=1) {
}else {
out.println("<a href='javascript:pre1Page()'>上一页</a>");
}
%>
</td>
</tr>
</table>
</td>
</tr>
</table>
</logic:greaterThan>
然后在Action的方法里面
设置一个PageItem类,需要什么属性,看页面就知道了吧,按自己需要的定制,就是一个Bean
设置 PageItem 的
在这之前
//设置分页条件
int currentPage = 1;
if(StringUtils.isBlank(***Form.getCurrentPage()+""))
{
currentPage = 1;
}else
{
currentPage = ***Form.getCurrentPage();
currentPage = (currentPage <= 0) ? 1 : currentPage;
}
设置查询条件
//查询代码方这里
*Form.setDataList(查询出來的结果);
PageItems pi = new PageItems();
pi.setSize(String.valueOf(PAGE_SIZE));
pi.setCurrentPage(String.valueOf(currentPage));
pi.setRecordSize(String.valueOf(count));
request.setAttribute("PageItems", pi);
return mapping.findForword("跳转显示页面");
使用方法是
在要(循环)显示的页面的表格前面加上这行
<logic:present name="PageItems">
<tr valign="top" colspan="5">
<td valign="top">
<!-- 就是上面那个页面 -->
<jsp:include page="pages.jsp" flush="yes" />
<script>
setForm(IvcIPSEForm);
</script>
</td>
</tr>
</logic:present>
还有一部分,在 service层的代码 *VO是作为条件传过来的javabean *DAOVO 是传到DAO层执行的javaBean
int currentPage = *VO.getCurrentPage();
int size =*VO.getSize();
int offset = (currentPage - 1) * size;
int length = ((offset + size) > count) ? (count - offset) : size;
*DAOVO.setOffset(offset);
*DAOVO.setLength(length);
......设置其他查询条件
在DAO里面
return this.getListForPage(sql.toString(), offset, length);
public List getListForPage(final String hql, final int offset, final int length) {
List retList = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return retList;
}
完成,呵呵