jsp中分页功能
Code
<table width="500" border="0">
<tr>
<td height="400" valign="top" ><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10"></td>
<td> </td>
</tr>
<tr>
<td colspan="2"><img src="images/productlist.jpg" width="377" height="44"> </td>
</tr>
<tr>
<td colspan="2" height="10"><%
List list = (List) request.getSession().getAttribute("productlist");
int size = list.size();//所得记录的条数
int counts ;//单页显示的条数
if(size<10)
{
counts=size;
int pages=1;
pageContext.setAttribute("pages", pages);
int pagesize;//当前的页码
String goToPage = "1";
List lists;
pagesize = Integer.parseInt(goToPage);
lists = list.subList(1, counts);//
pageContext.setAttribute("pagesize", pagesize);
pageContext.setAttribute("lists", lists);
}
else
{
counts=10;
int pages = (size % counts == 0) ? (size / counts)
: (size / counts + 1);//所需要显示的页数
pageContext.setAttribute("pages", pages);//设置所需显示的页数
int pagesize;//当前的页码
String goToPage = request.getParameter("goToPage");
List lists;
if (goToPage == null) {
goToPage = "1";
pagesize = Integer.parseInt(goToPage);
lists = list.subList(1, counts + 1);//
pageContext.setAttribute("pagesize", pagesize);
pageContext.setAttribute("lists", lists);
} else {
pagesize = Integer.parseInt(goToPage);
if (pagesize == 0) {
lists = list.subList(1, 1 + counts);//
pageContext.setAttribute("pagesize", 1);
pageContext.setAttribute("lists", lists);
} else if (pagesize == (pages + 1) || (pagesize == pages)) {//最后一页的显示
pagesize = pages;
lists = list.subList((pages - 1) * counts, size);//
pageContext.setAttribute("pagesize", pages);
pageContext.setAttribute("lists", lists);
} else {
lists = list.subList((pagesize * counts + 1 - counts),
(pagesize * counts + 1));//
pageContext.setAttribute("pagesize", pagesize);
pageContext.setAttribute("lists", lists);
}
}
}
%>
</td>
</tr>
<c:forEach items="${lists}" var="pro">
<tr>
<td width="10" valign="top"></td>
<td valign="top"><LI style="FONT-SIZE: 14px"> <a href="productlist.do?id=${pro.id}">${pro.name}</a> </LI></td>
</tr>
</c:forEach>
</table></td>
</tr>
<tr>
<td><table width="500" >
<tr>
<td width="13%" align="center"><a href="showAllProduct.jsp?goToPage=1"><span
class="style18">『首页』</span> </a> </td>
<td width="15%" align="center"><a href="showAllProduct.jsp?goToPage=${pagesize-1 }"><span
class="style18">『上一页』</span> </a> </td>
<td width="15%" align="center"><a href="showAllProduct.jsp?goToPage=${pagesize +1}"><span
class="style18">『下一页』</span> </a> </td>
<td width="13%" align="center"><a href="showAllProduct.jsp?goToPage=${pages }"><span
class="style18">『尾页』</span> </a> </td>
<td width="17%" align="center"><span class="style18"> 『第${pagesize }页』 </span> </td>
<td width="17%" align="center"><span class="style18"> 『共 ${pages } 页』 </span> </td>
</tr>
</table></td>
</tr>
</table>
<table width="500" border="0">
<tr>
<td height="400" valign="top" ><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10"></td>
<td> </td>
</tr>
<tr>
<td colspan="2"><img src="images/productlist.jpg" width="377" height="44"> </td>
</tr>
<tr>
<td colspan="2" height="10"><%
List list = (List) request.getSession().getAttribute("productlist");
int size = list.size();//所得记录的条数
int counts ;//单页显示的条数
if(size<10)
{
counts=size;
int pages=1;
pageContext.setAttribute("pages", pages);
int pagesize;//当前的页码
String goToPage = "1";
List lists;
pagesize = Integer.parseInt(goToPage);
lists = list.subList(1, counts);//
pageContext.setAttribute("pagesize", pagesize);
pageContext.setAttribute("lists", lists);
}
else
{
counts=10;
int pages = (size % counts == 0) ? (size / counts)
: (size / counts + 1);//所需要显示的页数
pageContext.setAttribute("pages", pages);//设置所需显示的页数
int pagesize;//当前的页码
String goToPage = request.getParameter("goToPage");
List lists;
if (goToPage == null) {
goToPage = "1";
pagesize = Integer.parseInt(goToPage);
lists = list.subList(1, counts + 1);//
pageContext.setAttribute("pagesize", pagesize);
pageContext.setAttribute("lists", lists);
} else {
pagesize = Integer.parseInt(goToPage);
if (pagesize == 0) {
lists = list.subList(1, 1 + counts);//
pageContext.setAttribute("pagesize", 1);
pageContext.setAttribute("lists", lists);
} else if (pagesize == (pages + 1) || (pagesize == pages)) {//最后一页的显示
pagesize = pages;
lists = list.subList((pages - 1) * counts, size);//
pageContext.setAttribute("pagesize", pages);
pageContext.setAttribute("lists", lists);
} else {
lists = list.subList((pagesize * counts + 1 - counts),
(pagesize * counts + 1));//
pageContext.setAttribute("pagesize", pagesize);
pageContext.setAttribute("lists", lists);
}
}
}
%>
</td>
</tr>
<c:forEach items="${lists}" var="pro">
<tr>
<td width="10" valign="top"></td>
<td valign="top"><LI style="FONT-SIZE: 14px"> <a href="productlist.do?id=${pro.id}">${pro.name}</a> </LI></td>
</tr>
</c:forEach>
</table></td>
</tr>
<tr>
<td><table width="500" >
<tr>
<td width="13%" align="center"><a href="showAllProduct.jsp?goToPage=1"><span
class="style18">『首页』</span> </a> </td>
<td width="15%" align="center"><a href="showAllProduct.jsp?goToPage=${pagesize-1 }"><span
class="style18">『上一页』</span> </a> </td>
<td width="15%" align="center"><a href="showAllProduct.jsp?goToPage=${pagesize +1}"><span
class="style18">『下一页』</span> </a> </td>
<td width="13%" align="center"><a href="showAllProduct.jsp?goToPage=${pages }"><span
class="style18">『尾页』</span> </a> </td>
<td width="17%" align="center"><span class="style18"> 『第${pagesize }页』 </span> </td>
<td width="17%" align="center"><span class="style18"> 『共 ${pages } 页』 </span> </td>
</tr>
</table></td>
</tr>
</table>