java 、jsp 一个简单的分页处理。最好代码参考

解决方案 »

  1.   

    去我的博客上看看,希望对你有所帮助
    http://blog.csdn.net/zxingchao2009/archive/2010/08/11/5805375.aspx
      

  2.   

    以下是一个最简单的分页,可以看看,希望对你有用:
    <%
       int pagesize=8;
       int currentPage=1;
       int showRows=8;
       DBBean dbbean=new DBBean();
       ResultSet rs=dbbean.query("select count(*) from notes");
       rs.next();
       int totalRows=rs.getInt(1)-1;
       int totalPages=totalRows/pagesize;
       if((totalRows-totalPages*pagesize)>0){
       totalPages=totalPages+1;
       }
       %>
       
        <% 
       if(request.getParameter("currentPage")!=null){
       int currentPageParameter=Integer.parseInt(request.getParameter("currentPage").trim());
       if(currentPageParameter>=1&&currentPageParameter<=totalPages)
       currentPage=currentPageParameter;
       if(currentPageParameter>totalPages)
       currentPage=currentPageParameter-1;
       }
      %>
      
      <body>
       <h2><img src="images/wordlist.bmp" width="40" height="40" />留言列表</h2><hr/>
       当前在第<span class="STYLE1"><%= currentPage%></span>页         总共<span class="STYLE2"><%=totalPages %></span>页
        <table width="80%" border="1" cellspacing="0">
          <tr>
    <td class="tdbgcolor">序号</td>
    <td class="tdbgcolor">标题</td>
    <td class="tdbgcolor">作者</td>
    <td class="tdbgcolor">内容</td>
    <td class="tdbgcolor">留言时间</td>
    <td class="tdbgcolor">管理员回复</td>
    <td class="tdbgcolor">操作</td>
      </tr>
      <%
        int firstshowRow=(currentPage-1)*pagesize+1-1;//注意limit的用法 limit a,b 也就是从a之后的b条记录,a不包括在内 
        int lastshowRow;
        if((firstshowRow+pagesize)>totalRows){
         lastshowRow=totalRows;
         showRows=lastshowRow-firstshowRow+1;
        }
        else lastshowRow=firstshowRow+pagesize-1;
        ResultSet rsnotes=(new DBBean()).query("select * from notes limit "+firstshowRow+","+showRows);   
        int no=firstshowRow+1;
       while(rsnotes.next()){      
       %>
       <tr>
         <td class="tdbgcolor"><%= no%></td>
         <td><%= rsnotes.getString("title")%></td>
         <td><%= rsnotes.getString("author")%></td>
         <td><%= rsnotes.getString("content")%></td>
         <td><%= rsnotes.getString("date") %></td>
         <td>
         <img alt="管理员" src="images/answerflag.bmp">
         <%
          String answerflag=(rsnotes.getString("answerflag").trim().equals("0"))?"未回复":"已回复" ; 
          %>
          <%=answerflag %>
         </td>
         <td><img alt="查看" src="images/look.bmp"><a href="looknotes.jsp?id=<%=rsnotes.getInt("id")%>" target="in">查看</a></td>
       </tr>
       <%
        no++;
        }
        %>
        </table>
        <p>
         <a href="list_notes.jsp?currentPage=1">首页</a>&nbsp;<a href="list_notes.jsp?currentPage=<%=currentPage-1 %>">上一页
         </a>&nbsp;跳转到第
         <select name="pageNumber" onchange='location.href= "list_notes.jsp?currentPage= "+this.value '>
         <%
         for(int i=1;i<=totalPages;i++){
         if(currentPage==i){    
          %>
           <option value=<%=i %> selected>
          <%
           }
           else{
           %>
            <option value=<%=i %> >
           <%
            }
            %>
          <%=i %></option>
          <%
           }
           %>
         </select>
         页
         <a href="list_notes.jsp?currentPage=<%=currentPage+1 %>">下一页</a>
         <a href="list_notes.jsp?currentPage=<%=totalPages%>">末页</a>
        </p>
        <%
         dbbean.close();
         %>
      

  3.   

    huanggangmaojin 她这个分页代码不错!
      

  4.   

    现在讲究的是代码复用性,将java代码写到Jsp页面功能虽然可以实现,但可复用性差,建议写个javabean封装分页的参数和页脚
      

  5.   

    <%
      int pagesize=8;
      int currentPage=1;
      int showRows=8;
      DBBean dbbean=new DBBean();
      ResultSet rs=dbbean.query("select count(*) from notes");
      rs.next();
      int totalRows=rs.getInt(1)-1;
      int totalPages=totalRows/pagesize;
      if((totalRows-totalPages*pagesize)>0){
      totalPages=totalPages+1;
      }
      %>
        
      <%  
      if(request.getParameter("currentPage")!=null){
      int currentPageParameter=Integer.parseInt(request.getParameter("currentPage").trim());
      if(currentPageParameter>=1&&currentPageParameter<=totalPages)
      currentPage=currentPageParameter;
      if(currentPageParameter>totalPages)
      currentPage=currentPageParameter-1;
      }
      %>
       
      <body>
      <h2><img src="images/wordlist.bmp" width="40" height="40" />留言列表</h2><hr/>
      当前在第<span class="STYLE1"><%= currentPage%></span>页 总共<span class="STYLE2"><%=totalPages %></span>页
      <table width="80%" border="1" cellspacing="0">
      <tr>
    <td class="tdbgcolor">序号</td>
    <td class="tdbgcolor">标题</td>
    <td class="tdbgcolor">作者</td>
    <td class="tdbgcolor">内容</td>
    <td class="tdbgcolor">留言时间</td>
    <td class="tdbgcolor">管理员回复</td>
    <td class="tdbgcolor">操作</td>
    </tr>
    <%
    int firstshowRow=(currentPage-1)*pagesize+1-1;//注意limit的用法 limit a,b 也就是从a之后的b条记录,a不包括在内  
    int lastshowRow;
    if((firstshowRow+pagesize)>totalRows){
    lastshowRow=totalRows;
    showRows=lastshowRow-firstshowRow+1;
    }
    else lastshowRow=firstshowRow+pagesize-1;
    ResultSet rsnotes=(new DBBean()).query("select * from notes limit "+firstshowRow+","+showRows);   
    int no=firstshowRow+1; 
    while(rsnotes.next()){ 
    %>
    <tr>
    <td class="tdbgcolor"><%= no%></td>
    <td><%= rsnotes.getString("title")%></td>
    <td><%= rsnotes.getString("author")%></td>
    <td><%= rsnotes.getString("content")%></td>
    <td><%= rsnotes.getString("date") %></td>
    <td>
    <img alt="管理员" src="images/answerflag.bmp">
    <%
    String answerflag=(rsnotes.getString("answerflag").trim().equals("0"))?"未回复":"已回复" ;  
    %>
    <%=answerflag %>
    </td>
    <td><img alt="查看" src="images/look.bmp"><a href="looknotes.jsp?id=<%=rsnotes.getInt("id")%>" target="in">查看</a></td>
    </tr>
    <%
    no++;
    }
    %>
      </table>
      <p>
      <a href="list_notes.jsp?currentPage=1">首页</a>&nbsp;<a href="list_notes.jsp?currentPage=<%=currentPage-1 %>">上一页
      </a>&nbsp;跳转到第
      <select name="pageNumber" onchange='location.href= "list_notes.jsp?currentPage= "+this.value '>
      <%
      for(int i=1;i<=totalPages;i++){
      if(currentPage==i){ 
      %>
      <option value=<%=i %> selected>
      <%
      }
      else{
      %>
      <option value=<%=i %> >
      <%
      }
      %>
      <%=i %></option>
      <%
      }
      %>
      </select>
      页
      <a href="list_notes.jsp?currentPage=<%=currentPage+1 %>">下一页</a>
      <a href="list_notes.jsp?currentPage=<%=totalPages%>">末页</a>
      </p>
      <% 
      dbbean.close();
      %>
      

  6.   

    总的来说,分页这玩意儿手写还是太麻烦了,直接用分页的组件吧要是数据量少的话,用displaytag,数据量大的话,用pager-taglib,多好啊,方便,还能有很多样式
      

  7.   

    写个公用的分页的javabean,方便重复调用lz可以直接百度,应该有很多的
      

  8.   

    写个java类,然后页面调用JavaBean,分页时用sql文控制条数如:select * from (select mybook.*,rownum as my_rownum from (select * from book order by booknum)mybook where rownum<6) where my_rownum>=1;
      

  9.   

    http://blog.csdn.net/zxingchao2009/archive/2010/08/11/5805375.aspx
      

  10.   

    我倒是有一套java + oracle 的分页方案,重用性很强,也没有多复杂,就一个java类,包括了后台分页处理和前台显示。楼主若感兴趣可以发给你参考。
      

  11.   

    当然,我那个是基于struts2的,在strut2下面才更能体现简单。
      

  12.   

    如果用session 保存查询列表数据 。
     一般这样吗? 除了session 时效性 ,这样好吗
      

  13.   


    我想看看,能不能发个啊![email protected]先谢谢啦!
      

  14.   


    晕,,引用错了我想看看,能不能发个啊![email protected]先谢谢啦!
      

  15.   

    jsp+servlet+javabean ,不知LZ是否需要
      

  16.   

    package t1;
    import java.io.Serializable;public class BookBean  { /**
     * 
     */
    private static final long serialVersionUID = 1L; private String ISBN;// ISBN编号 private String title;// 书名 private String copyright;// 版权 private String imageFile;// 封面图像 private int editionNumber;// 版本 private int publisherID;// 出版商ID private double price;// 价格 /**
     * 返回版权
     * 
     * @return
     */
    public String getCopyright() {
    return copyright;
    } /**
     * 设定版权
     * 
     * @param copyright
     */
    public void setCopyright(String copyright) {
    this.copyright = copyright;
    } /**
     * 返回版本
     * 
     * @return
     */
    public int getEditionNumber() {
    return editionNumber;
    } /**
     * 设定版本
     * 
     * @param editionNumber
     */
    public void setEditionNumber(int editionNumber) {
    this.editionNumber = editionNumber;
    } /**
     * 返回封面图像文件名
     * 
     * @return
     */
    public String getImageFile() {
    return imageFile;
    } /**
     * 设定封面图像文件名
     * 
     * @param imageFile
     */
    public void setImageFile(String imageFile) {
    this.imageFile = imageFile;
    } /**
     * 返回ISBN编号
     * 
     * @return
     */
    public String getISBN() {
    return ISBN;
    } /**
     * 设定ISBN编号
     * 
     * @param isbn
     */
    public void setISBN(String isbn) {
    ISBN = isbn;
    } /**
     * 返回价格
     * 
     * @return
     */
    public double getPrice() {
    return price;
    } /**
     * 设定价格
     * 
     * @param price
     */
    public void setPrice(double price) {
    this.price = price;
    } /**
     * 返回出版商ID
     * 
     * @return
     */
    public int getPublisherID() {
    return publisherID;
    } /**
     * 设定出版商ID
     * 
     * @param publisherID
     */
    public void setPublisherID(int publisherID) {
    this.publisherID = publisherID;
    } /**
     * 返回书名
     * 
     * @return
     */
    public String getTitle() {
    return title;
    } /**
     * 设定书名
     * 
     * @param title
     */
    public void setTitle(String title) {
    this.title = title;
    } public String toString() {
    return this.title;
    }
    }
    package t1;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;public class TitlesDAO {
    private Connection con = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    int perSize = 3;
    int totalPage = 0; public List getAll(int pageNum) {
    List list = new ArrayList();
    int rowBegin = 0;
    if (pageNum > 1) {
    rowBegin = (pageNum - 1) * perSize;
    }
    String sql = "select top  3 * from titles where isbn not in (select top "
    + rowBegin
    + " isbn from titles order by isbn desc) order by isbn desc";
    con = ConnectionManager.getCon();
    try {
    pstmt = con.prepareStatement(sql);
    rs = pstmt.executeQuery();
    while (rs.next()) {
    BookBean bb = new BookBean();
    bb.setISBN(rs.getString("isbn"));
    bb.setTitle(rs.getString("title"));
    bb.setPrice(rs.getDouble("price"));
    list.add(bb);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } return list;
    } public int getTotal() {
    int temp = 0;
    String sql = "select count(*) from titles";
    con = ConnectionManager.getCon();
    try {
    pstmt = con.prepareStatement(sql);
    rs = pstmt.executeQuery();
    while (rs.next()) {
    temp = rs.getInt(1);
    }
    /**
     * 如果有余数(总条数 % 每页显示的记录数) 总页数 = 总条数 / 每页显示的记录数 +1; 如果没有余数 总页数 = 总条数 /
     * 每页显示的记录数
     */ } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if (temp % perSize != 0) {
    return this.totalPage = temp / perSize + 1;
    } else {
    return this.totalPage = temp / perSize;
    } } public static void main(String[] args) {
    System.out.println(new TitlesDAO().getTotal());
    } public int getTotalPage() {
    return totalPage;
    }
    }<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%@page import="t1.TitlesDAO"%>
    <%@page import="t1.BookBean"%>
    <%
    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>
    <%
    String pageStr = request.getParameter("page");
    int pageNum = 1;
    int next = 1;
    int pre = 1; if (null != pageStr) {
    try {
    pageNum = Integer.parseInt(pageStr);
    } catch (Exception e) { }
    }
    TitlesDAO td = new TitlesDAO();
    List list = td.getAll(pageNum);
    td.getTotal();
    if (pageNum <= td.getTotalPage()) {
    next = pageNum + 1;
    if (pageNum == td.getTotalPage()) {
    next = pageNum;
    }
    }
    if (pageNum > 1) {
    pre = pageNum - 1;
    }
    %>
    总共有:<%=td.getTotal()%>

    <Br />
    <a href="index.jsp">首页</a>
    <a href="index.jsp?page=<%=pre%>">上一页</a> &nbsp;
    <a href="index.jsp?page=<%=next%>">下一页</a>
    <a href="index.jsp?page=<%=td.getTotalPage() %>">尾页</a>
    <br />
    <%
    for (int i = 0; i < list.size(); i++) {
    BookBean bb = (BookBean) list.get(i);
    %>
    ISBN:<%=bb.getISBN()%>
    || Title:<%=bb.getTitle()%>
    || Price:<%=bb.getPrice()%>
    <Br /> <%
    }
    %>
    </body>
    </html>