JSP中要从数据库里面读取数据 每页显示5条数据,要进行翻页,还要返回前一页 该怎么实现啊

解决方案 »

  1.   

    5. 实现数据访问对象ArticleDAOImpl.java
    /************************************************* 
    * @author     孙丰伟 E-mail: [email protected] 
    *                Mobile:    13894850239 
    *                Blog:    http://hi.baidu.com/j2ee_cn
    * @version     创建时间:2009-6-30 下午07:26:23 
    * @see 
    *************************************************/
    package com.sfw.board.bll.impl;import java.sql.SQLException;
    import java.sql.Types;
    import java.util.ArrayList;
    import java.util.List;import com.sfw.board.bll.ArticleDAO;
    import com.sfw.board.model.Article;
    import com.sfw.board.model.Category;import commons.BaseDAO;public class ArticleDAOImpl extends BaseDAO implements ArticleDAO {    private int pageSize = 3;
        private String queryWhere;
        private Article article;
        private int totalPages;    
        /**
        * 
            @tableName nvarchar(255),      -- 表名或视图表
            
            @fieldList nvarchar(1000) = '*', -- 字段列表
            
            @orderRule nvarchar(255)='',     -- 排序规则
            
            @pageSize   int = 10,        -- 每页行数
            
            @pageIndex int = 1,             -- 当前页码
            
            @queryWhere nvarchar(1500) = '', -- 查询条件(注意: 不要加where)
            
            @keyId nvarchar(50),                --主键
            
            @totalPages int output                --总页数
        */
        public List getArticlesByPageIndex(int currentPage) {
            List<Article> articles = new ArrayList<Article>();
            try {
                cstmt = conn.prepareCall("{call up_commonShowPage2000(?,?,?,?,?,?,?,?)}");
                cstmt.setString(1, "view_Category_Article");
                cstmt.setString(2, " id,title,publishTime,authorId,categoryid,name,browserCount,commentCount ");
                cstmt.setString(3, "order by ID");
                cstmt.setInt(4, pageSize); // 第页行数
                cstmt.setInt(5, currentPage); // 当前页码
                cstmt.setString(6, queryWhere);
                cstmt.setString(7, "ID");
                cstmt.registerOutParameter(8,Types.INTEGER);
                rs = cstmt.executeQuery();
                while (rs.next()) {
                    article = new Article();
                    article.setId(rs.getInt("ID"));
                    article.setTitle(rs.getString("title"));
                    article.setPublishTime(rs.getString("publishTime"));
                    article.setBrowserCount(rs.getInt("browserCount"));
                    article.setCommentCount(rs.getInt("commentCount"));
                    article.setCategory(new Category(rs.getInt("categoryId"), rs.getString("name")));
                    articles.add(article);
                }
                
                //通过输出参数求部页数
                
                this.totalPages=cstmt.getInt(8);
                
                rs.close();
                cstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }        return articles;
        }    /**
        * @return the pageSize
        */
        public int getPageSize() {
            return pageSize;
        }    /**
        * @param pageSize
        *            the pageSize to set
        */
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }    /**
        * @return the queryWhere
        */
        public String getQueryWhere() {
            return queryWhere;
        }    /**
        * @param queryWhere
        *            the queryWhere to set
        */
        public void setQueryWhere(String queryWhere) {
            this.queryWhere = queryWhere;
        }    /**
        * @return the article
        */
        public Article getArticle() {
            return article;
        }    /**
        * @param article
        *            the article to set
        */
        public void setArticle(Article article) {
            this.article = article;
        }6. 创建实现翻页的公共Bean : pageControl.javapackage commons; /***********************************************************
    * 类编号: 
    * 类 名: PageControl
    * 作 用: 通用控制翻页类 
    * 作 者: 孙丰伟 
    * 日 期: 2008-6-13
    * 构造方法: 
    *         
    * 方 法: 
    ************************************************************/
    public class PageControl {
    private int currentPage=1;        //当前页
    private int totalPages=-1;        //总页数
    private int pageSize=-1;        //每页行数
    public PageControl()
    {
    }
    /********************************************************
    * 函数名:     构造方法 
    * 功能描述: 实例翻页相关属性 
    * 输入参数: 
    * @param currentPage
    * @param pageSize
    * @param totalRecords
    * 返 回 值: 无
    ********************************************************/
    public PageControl(int currentPage,int pageSize,int totalPages)
    {
        this.currentPage=currentPage;
        this.pageSize=pageSize;
        this.totalPages=totalPages;
    }
    /********************************************************
    * 函数名:     getPageControl 
    * 功能描述: 计算并取翻页链接 
    * 输入参数: 需翻页的URL
    * 返 回 值: 翻页链接
    ********************************************************/public String getPageControl(String pageURL)
    {
        StringBuilder linkURL=new StringBuilder();    //System.out.println(this.totalPages);
        String firstLink="<a href='"+pageURL+"?currentPage=1'>第一页|</a>";
        String previousLink="<a href='"+pageURL+"?currentPage="+(currentPage-1)+"'>上一页|</a>";
        String nextLink="<a href='"+pageURL+"?currentPage="+(currentPage+1)+"'>下一页|</a>";
        String lastLink="<a href='"+pageURL+"?currentPage="+totalPages+"'>最后一页</a>";;
        
        
        if(this.currentPage<=1)
        {
            return linkURL.append(nextLink).append(lastLink).toString();
        }
        if(this.currentPage>=totalPages)
        {
            return linkURL.append(firstLink).append(previousLink).toString();
        }
        return linkURL.append(firstLink).append(previousLink).append(nextLink).append(lastLink).toString();
    }
    }7. JSP实现:index.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import ="com.sfw.board.bll.impl.*"%>
    <%@ page import ="com.sfw.board.model.*"%>
    <%@ page import ="commons.*"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    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>
    <table width="970" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="10" rowspan="2">&nbsp;<img src="images/header_1.gif" width="10" height="66"></td>
          <td height="30" background="images/header_bg.gif">&nbsp;</td>
          <td width="10" rowspan="2"><img src="images/header_2.gif" width="10" height="66"></td>
        </tr>
        <tr>
          <td background="images/header_bg.gif">&nbsp;</td>
        </tr>
    </table>
    <jsp:useBean id="articleDAO" class="com.sfw.board.bll.impl.ArticleDAOImpl"></jsp:useBean>
    <jsp:useBean id="user" class="com.sfw.board.model.User" scope="session"></jsp:useBean>
    <%
            user.setId(2);
            String cPage= request.getParameter("currentPage");
            int currentPage = null==cPage?1:Integer.parseInt(cPage);
            // User user = (User)session.getAttribute("user");
            articleDAO.setPageSize(3);
            articleDAO.setQueryWhere("authorId="+user.getId());
            articleDAO.open();
            List articles = articleDAO.getArticlesByPageIndex(currentPage);
            pageContext.setAttribute("articles",articles);
            articleDAO.close();
            //int currentPage,int pageSize,int totalRecords
            PageControl pageControl = new PageControl(currentPage,3,28);
            
    %>
    <table border="1" width="90%">
    <tr>
    <th>ID</th>
    <th>标题</th>
    <th>时间</th>
    </tr>
    <c:forEach var="r" items="${articles}">
    <tr>
    <td>${r.id }</td>
    <td>${r.title }</td>
    <td>${r.publishTime }</td>
    </tr>
    </c:forEach></table>
    <hr>
    <%=pageControl.getPageControl(basePath+"index.jsp") %><hr>
        <p>This is my JSP page. <br>
          <select name="webUrl" onChange="window.location=this.value">
            <option value="http://hi.baidu.com/j2ee_cn">孙丰伟</option>
            <option value="http://www.baidu.com">百度</option>
            <option value="http://www.gougou.com/">狗狗</option>
            <option value="http://www.google.com">Google</option>
                                        </select>
        
                  </p>
    </body>
    </html>
      

  2.   

    http://my.donews.com/benz/2006/10/21/YnmQXKsKWNgKiTJOvWrATVNevmgMIdOHqbxo/