最近碰到了一个问题就是:在同一个JSP页面里面,当用户第一次进来的时候。需要显示的是所有的记录数(分页显示),如果用户选择了条件,后面就应该显示根据用户选择的条件进行分页而在JSP里面固定的上一页 或者 下一页  的链接还是原来显示所有的连接。这个问题该怎么优化(处理)?
部分代码如下:
 <c:if test="${showPage>1}">
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=1">首页</a>
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${showPage-1}">上一页</a>
</c:if>
<c:if test="${showPage<maxPage || showPage==maxPage}">
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${showPage+1}">下一页</a>
        <a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${maxPage}">尾页</a>
</c:if>第<font color='#FF0000'>${showPage}</font>页&frasl; 共<font color='#FF0000'>${maxPage}</font>页
         <a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=1">返回所有</a>

解决方案 »

  1.   

    点 上一页 或下一页的链接的时候,再跑一边action
      

  2.   

    专门建一个Page.java页面分页的管理类,再建立一个分页显示的辅助类PageHelp.java来进行上一页,下一页的功能的实现,在action当中分别进行调用相应的方法就可以进行实现了喽
      

  3.   

    在selectAllMessageInfoPage中将用户选择的条件(或者搜索出来的结果,但是不建议)放在session中
      

  4.   

    点击按钮的时候肯定是要调用action的,再在action中调用相应的方法啊
      

  5.   

    你的条件是通过链接选择的吧?如果是可以添加链接参数,改变分页代码中的SQL语句。
      

  6.   


    3楼的,用CSS如何实现?发一下代码.
      

  7.   

    点击按钮走Action将要跳转的页数也作为参数传递过去。
      

  8.   

    点链接再走一遍action不就可以了么~
      

  9.   

    补充: 我这个页面里面的第一进来的时候。要能上一页 下一页。得可以显示相应的全部的。 当有了条件之后才根据条件来上一页。或下一页的
                  一、第一次进来:
                              要分页显示全部的。并能实现上一页。下一页
             
                  二、当用户选择了条件后。
                              要根据用户选中的条件来分页显示上一页,下一页。。
                        这个地方的分页 sql 语句怎么写? 全部的可以。当是在原来的基础上不能加入条件。这个问题 怎么解决呀。还请大家帮帮忙? 谢谢了
      

  10.   

    想先问下楼主使用的是什么底层拿数据库资源,是jdbc,还是hibernate.
    如果是hibernate,楼主只需要改变你想看的页数就可以了,不需要条件的。如果是jdbc,因为每一次查询都需要条件,所以每次传入页码的时候,还是要把条件传过去就可以了啊。
    想不到有什么问题。
      

  11.   

    我的资源里有个javabean分页源码,注释的相当详细。运行ok
    有需要的朋友可以去看看
      

  12.   

    <%@ page contentType="text/html; charset=gb2312" language="java"
    import="java.sql.*" errorPage=""%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="data.*"%>
    <jsp:useBean id="pages" scope="page" class="data.strongSplitPage" />
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%!//显示每页的纪录数
    int pageSize = 5;
    String sqlStr = "";
    //当前页
    int showPage = 1; String cloth = "";
    String clothes = "";
    String search = "";
    String searches = "";%><%
    if (request.getParameter("search_info") != null)
    cloth = request.getParameter("search_info");
         
    if (request.getParameter("info_Name") != null)
    search = request.getParameter("info_Name");
    try {
    clothes = new String(cloth.getBytes("ISO-8859-1"), "gbk");

    searches = new String(search.getBytes("ISO-8859-1"), "gbk");

    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } //str = "select * from clothes";
    if (clothes.equals("服饰")) {
    //str = "select * from clothes";
    if (search.equals("") || search == null) {
    sqlStr = "select * from clothes";
    } else {
    sqlStr = "select  * from clothes where clothes_Name  like '%"
    + searches + "%'";
    }
    } else if (clothes.equals("美食")) {
    //str = "select * from food";
    if (search.equals("") || search == null) {
    sqlStr = "select * from food";
    } else {
    sqlStr = "select  * from food where food_Name  like '%"
    + searches + "%'";
    }
    }  String strPage = null;
    //获得跳转到的页面  
    strPage = request.getParameter("showPage");
    if (strPage == null) {
    showPage = 1;
    pages.initialize(sqlStr, pageSize, showPage);
    } else {
    try {
    showPage = Integer.parseInt(strPage);
    pages.initialize(sqlStr, pageSize, showPage);
    } catch (NumberFormatException ex) {
    showPage = 1;
    pages.initialize(sqlStr, pageSize, showPage);
    }
    if (showPage < 1) {
    showPage = 1;
    pages.initialize(sqlStr, pageSize, showPage);
    }
    if (showPage > pages.getPageCount()) {
    showPage = pages.getPageCount();
    pages.initialize(sqlStr, pageSize, showPage);
    }
    }
    //取得要显示的数据集合
    Vector vData = pages.getPage();
    %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>分页显示</title>
    </head> <body bgcolor="#ffffff" text="#000000">
    <h1 align=center>
    个人基本信息
    </h1>
    <div align=center>
    <table border="1" cellspacing="0" cellpadding="0" width="80%">

    <%
    for (int i = 0; i < vData.size(); i++) {
    //显示数据数
    String[] sData = (String[]) vData.get(i);
    %>
    <tr>
    <td><%=sData[0]%></td>
    <td><%=sData[1]%></td>
    <td><%=sData[2]%></td>
    </tr>
    <%
    }
    %>
    </table>
    <p>
    <form action="MyJsp.jsp" method="get" target="_self">
    <p>

    <font color=red><%=pages.getRowCount()%></font>条&nbsp;<%=pageSize%>条/页&nbsp;&nbsp;第
    <font color=red><%=showPage%></font>页/共
    <font color=red><%=pages.getPageCount()%></font>页&nbsp;&nbsp;[
    <a href="MyJsp.jsp?showPage=1" target="_self">首页</a>]&nbsp;
    <%
    //判断“上一页”链接是否要显示
    if (showPage > 1) {
    %>
    [
    <a href="MyJsp.jsp?showPage=<%=showPage - 1%>" target="_self">上一页</a>]&nbsp;
    <%
    } else {
    %>
    [上一页]&nbsp;
    <%
    }
    //判断“下一页”链接是否显示
    if (showPage < pages.getPageCount()) {
    %>
    [
    <a href="MyJsp.jsp?showPage=<%=showPage + 1%>" target="_self">下一页</a>]&nbsp;
    <%
    } else {
    %>
    [下一页]&nbsp;
    <%
    }
    %> [
    <a href="MyJsp.jsp?showPage=<%=pages.getPageCount()%>"
    target="_self">尾页</a>]&nbsp;转到
    <select name="select">
    <%
    for (int x = 1; x <= pages.getPageCount(); x++) {
    %>
    <option value="<%=x%>"
    <%
              if(showPage==x){
               out.println("selected");
            }   
          %>><%=x%></option>
    <%
    }
    %>
    </select>
    页&nbsp;&nbsp;&nbsp;
    <input type="submit" name="go" value="提交" />
    </p>
    </form>
    </p>
    </div>
    </body>
    </html>
    package data;import java.sql.*;
    import java.util.*;public class strongSplitPage {
    private Connection conn = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private ResultSetMetaData rsmd = null;
    // sql 查询语句
    private String sqlStr;
    // 总纪录数目
    private int rowCount;
    // 所分得逻辑页数
    private int pageCount;
    // 每页显示的纪录数目
    private int pageSize;
    // 定义表的列数目
    private int columnCount;
    private int irows; public void initialize(String sqlStr, int pageSize, int showPage) {
    this.sqlStr = sqlStr;
    this.irows = pageSize * (showPage - 1);
    this.pageSize = pageSize;
    try {
    LoginData loginData = new data.LoginData();
    this.conn = loginData.getConn();
    this.stmt = this.conn.createStatement();
    this.rs = this.stmt.executeQuery(this.sqlStr);
    this.rsmd = this.rs.getMetaData();
    if (this.rs != null) {
    this.rs.last();
    this.rowCount = this.rs.getRow();
    this.rs.first();
    this.columnCount = this.rsmd.getColumnCount();
    this.pageCount = (this.rowCount - 1) / this.pageSize + 1;
    this.rs.close();
    this.stmt.close();
    }
    this.sqlStr = this.sqlStr + " limit " + this.irows + ","
    + this.pageSize;
    this.stmt = this.conn.createStatement();
    this.rs = this.stmt.executeQuery(this.sqlStr);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    } public Vector getPage() {
    Vector vData = new Vector();
    try {
    if (this.rs != null) { while (this.rs.next()) {
    String[] sData = new String[this.columnCount];
    for (int j = 0; j < this.columnCount; j++) {
    sData[j] = this.rs.getString(j + 1);
    }
    // 对 向 量 增加
    vData.addElement(sData);
    }
    this.rs.close();
    this.stmt.close();
    this.conn.close();
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return vData;
    } // 获得页面总数
    public int getPageCount() {
    return this.pageCount;
    } // 获得数据表中总纪录数
    public int getRowCount() {
    return this.rowCount;
    }
    }
    package data;import java.sql.*;public class LoginData{
        Connection conn=null; 
        public LoginData(){
                  this.connect();    
        }
       
        public Connection getConn(){
                return this.conn;
        }
        public boolean connect(){
               try{
              //使用JDBC桥创建数据库连接
           Class.forName("com.mysql.jdbc.Driver").newInstance();
            
         //使用DriverManager类的getConnection()方法建立连接
         //第一个参数定义用户名,第二个参数定义密码
         this.conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?useUnicode=true&amp;characterEncoding=gb2312","root","root");
          }catch(Exception ex){
               ex.printStackTrace(); 
         return false;
          }
          return true;
        }
    }