本帖最后由 段旭东 于 2013-5-16 10:15 编辑
jsp也面的分页显示 我只知道有两种:一种是sql分页 sql语句可以限制 每次查询时显示的数量,一种是 后台处理 不过这两种 都需要用到 jstl标签 <C:foreach> 才能循环显示
这是一个例子:前两个是 servlet 算是专门测试 分页的 小例子 你要是觉得 看不懂 我也可以把原项目 发给你!暂时 jdbc阶段 你只需要连接数据库 操纵增删改查 就足够了!慢慢来哦- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.List;
- import javax.el.ELContext;
- import javax.servlet.jsp.JspContext;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.tagext.SimpleTagSupport;
- import com.sun.faces.spi.ManagedBeanFactory.Scope;
- public class PartPageTag extends SimpleTagSupport {
- private List source;// 被分页的集合接口
- private String result;// 分页后的子集合
- private String size;// 每页显示的数据数量
- private String currentPage;// 当前页码
-
- private int pageQuantity; // 总的分页数
-
- public PartPageTag(){
- System.out.println("实例化标签实现类对象");
-
- }
-
- @Override
- public void doTag() throws JspException, IOException {
- // TODO Auto-generated method stub
- super.doTag();
-
- JspContext jspContext=this.getJspContext();// 获得JspContext对象
-
- int sum=source.size();// 获得集合大小
- System.out.println("一共有数据:"+sum);
- /*
- * 判断每页显示的数据数量是否大于源集合中的数据数量
- * */
- List list=new ArrayList();//封装最后分页数据的集合
-
- if(size==null || size.equals("")){// 如果此属性缺省或者是空值则不对集合进行分割
-
- System.out.println("缺省数量");
- list=source;
-
- }else{// 如果size属性填入值,则进行比较
-
- //判断每页显示的数量是否大于等于源集合大小
- int num=Integer.parseInt(size);
- if(num>=source.size()){// 如果大于或等于原集合大小则不分页
- list=source;
-
- }else{
- pageQuantity=evalPagination(num);// 调用计算总页码数的方法
- validatePage();// 调用验证页码是否在合法的范围方法
- list=partitionSourceList(num);//调用分割集合的方法
- }
- }
- jspContext.setAttribute("currentPage",currentPage, 2);// 把当前页码放入result作用域中
- jspContext.setAttribute("pageQuantity",pageQuantity, 2);// 把当前页码总数放入result作用域中
-
- jspContext.setAttribute(result, list, 2);// 把分页后数据封装到list中并以result标签属性作为引用存放在请求域中
- }
-
- /*计算总页码数的方法*/
-
- private int evalPagination(int num){
- System.out.println("计算总的页码数");
- int sum;
- sum=source.size()/num;// 总页数变量(集合大小除以每页显示的数据数)
- //判断是否能够整除
- if(source.size()%num !=0){// 如果不能够除尽则在总页数上加1,多显示一页
- sum=sum+1;
- }
- return sum;
- }
-
- /*验证页码是否在合法的范围方法*/
-
- private void validatePage(){
- System.out.println("检验页码是否正确");
- // 如果页码属性currentPage 为null,为 空字符串,为字符串 0
- if(currentPage==null||currentPage.equals("")||currentPage.equals("0"))
- currentPage="1";// 设为第一页
- //如果页码属性currentPage大于等于总的页码数
- else if(Integer.parseInt(currentPage)>=pageQuantity){
- currentPage=String.valueOf(pageQuantity);// 设为最后一页
-
- }
- }
-
- /*分割集合的方法
- * */
- private List partitionSourceList(int num){
-
- System.out.println("分割集合");
- List res=new ArrayList();
-
- if(Integer.parseInt(currentPage)>=pageQuantity){
-
- if(source.size()%num ==0){
- res=source.subList(Integer.parseInt(currentPage)
- *Integer.parseInt(size)-Integer.parseInt(size),
- Integer.parseInt(currentPage)
- *Integer.parseInt(size)-Integer.parseInt(size)+Integer.parseInt(size));
-
- }else{
- res=source.subList(Integer.parseInt(currentPage)
- *Integer.parseInt(size)-Integer.parseInt(size),
- pageQuantity
- *Integer.parseInt(size)-Integer.parseInt(size)+source.size()%num);
-
- }
-
- }
- else{
- res=source.subList(Integer.parseInt(currentPage)
- *Integer.parseInt(size)-Integer.parseInt(size),
- Integer.parseInt(currentPage)
- *Integer.parseInt(size)-Integer.parseInt(size)+Integer.parseInt(size));
- }
- return res;
- }
- /*
- * getter and setter methods ...
- * */
- public List getSource() {
- return source;
- }
- public void setSource(List source) {
- this.source = source;
- }
-
- public String getResult() {
- return result;
- }
- public void setResult(String result) {
- this.result = result;
- }
-
- public String getSize() {
- return size;
- }
- public void setSize(String size) {
- this.size = size;
- }
-
- public String getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(String currentPage) {
- this.currentPage = currentPage;
- }
-
- }
复制代码- import java.io.IOException;
- import javax.servlet.jsp.JspContext;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspWriter;
- import javax.servlet.jsp.tagext.SimpleTagSupport;
- public class PagingLink extends SimpleTagSupport {
- @Override
- public void doTag() throws JspException, IOException {
- // TODO Auto-generated method stub
- super.doTag();
- JspContext context=this.getJspContext();// 获得JspContext对象
- JspWriter out=context.getOut();// 获得JspWriter对象
-
- int current=Integer.parseInt(String.valueOf(context.getAttribute("currentPage", 2)));
- int pagesum=Integer.parseInt(String.valueOf(context.getAttribute("pageQuantity", 2)));
-
- out.print("<a href='?page=1'>首页</a> ");
- out.print("<a href='?page="+(current-1)+"'>上一页</a>");
- out.print(" 一共 "+pagesum+" 页");
- out.print(" 当前页码是第 "+(current)+" 页 ");
- out.println("<a href='?page="+(current+1)+"'>下一页</a>");
- out.print(" <a href='?page="+pagesum+"'>尾页</a>");
- }
-
- }
复制代码 这个是 jsp页面的代码- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
- <%@ taglib uri="/WEB-INF/tag" prefix="p" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
-
- <body>
- This is my JSP page.
- <%
-
- pageContext.setAttribute("user",new Object());
- Collection conn=new ArrayList();
-
- conn.add(new String("KING"));
- conn.add(new String("CAO"));
- conn.add(new String("TOMCAT"));
- conn.add(new String("LUCY"));
- conn.add(new String("TOMCAT"));
- conn.add(new String("LUCY"));
- conn.add(new String("TOMCAT"));
- conn.add(new String("LUCY"));
- conn.add(new String("TOMCAT"));
- conn.add(new String("LUCY"));
- conn.add(new String("TOMCAT"));
- conn.add(new String("反对法"));
- conn.add(new String("反对福建卡"));
- conn.add(new String("oorereoo"));
- conn.add(new String("45rere6"));
- conn.add(new String("12rere3"));
- conn.add(new String("TOMCreAT"));
- conn.add(new String("反对rrere法"));
- conn.add(new String("反对福rerere建卡"));
- conn.add(new String("ooorerereo"));
- conn.add(new String("4rerere56"));
- conn.add(new String("fdfddf"));
-
- request.setAttribute("data",conn);
- %>
- <p:partition source="${requestScope.data}" result="a" currentPage="${param.page}" size="7"/>
-
-
-
- <center >
- <table border="1" cellpadding="5" cellspacing="1">
- <c:forEach items="${a}" var="obj">
- <tr><td>${obj}</td></tr>
- </c:forEach>
- <tr style="font-size: 13"><td><p:pagingLink/></td></tr>
- </table>
- </center>
-
-
- </body>
- </html>
复制代码 |