每页显示两行,一行有四张图片,第一页显示出来了,关键是点击下一页(图片)内容没有变化

解决方案 »

  1.   


      package com.page;
    import java.util.*;
    import com.page;
    import java.sql.*;
    public class ContactBean{
    private Connection con;
    Vector v;

    public ContactBean()throws Exception{
    conn=DatabaseConn.getConnection();
    v=new Vector();
    }
    public int getAvailableCount()throws Exception{
    int ret=0;
    Statement stmt = con.createStatement();
    String strSql = "select count(*) from contact";
    ResultSet rs = stmt.execute(strSql);
    while(rs.next()){
    ret=rs.getInt(1);
    }
    return ret;
    }
    public PageBean listData(String page)throws Exception{
    try{
    PageBean pageBean = new PageBean(this);
    int pageNum = Integer.parseInt(page);
    Statement stmt = con.createStatement();

    String strSql = "select top"+pageNum*pageBean.rowsPerPage+" * from contact order by userName";
    ResultSet rs=stmt.executeQuery(strSql);
    int i=0;
    while(rs.next()){
    if(i>(pageNum-1)*pageBean.rowsPerPage-1){
    Object[] obj = new Object[6];
    obj[0] = rs.getString("userName");
    obj[1] = new Integer(rs.getInt("mobile"));
    obj[2] = rs.getString("phone");
    obj[3] = rs.getString("mail");
    obj[4] = rs.getDate("lastcontact");
    obj[5] = rs.getString("mem");
    v.add(obj);
    }
    i++;
    }
    rs.close();
    stmt.close();
    pageBean.curPage=pageNum;
    pageBean.data=v;
    return pageBean;

    }catch(Exception e){
    e.printStackTrace();
    }
    }
    public Vector getResult()throws Exception{
    return v;
    }
    }
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class ContactServlet extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletExcetption,IOException{
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    try{
    ContactBean contact = new ContactBean();
    PageBean pageCtl = new contact.listData((String)request.getParameter("jumpPage"));
    request.setAttribute("pageCtl",pageCtl);

    }catch(Exception se){
    se.printStackTrace();
    }
    RequestDispatcher dis = request.getRequestDispatcher("/viewcontact");
    dis.forward(request,response);
    }

    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletExcetption,IOException{
       doGet(request,response);
    }
    }package com.page;
    import java.util.Vector;
    public class PageBean{
    public int curPage;
    public int maxPage;
    public int maxRowCount;
    public int rowsPerPage=5;
    public Vector data;
       public PageBean(){
       
       }
       public void countMaxPage(){
        if(this.maxRowCount%this.rowsPerPage==0){
        this.maxPage=this.maxRowCount/this.rowsPerPage;
        }else{
        this.maxPage=this.maxRowCount/this.rowsPerPage+1;
        }
       }
       public Vector getResult(){
        return this.data;
       }
       public PageBean(ContactBean contact)throws Exception{
         this.maxRowCount=contact.getAvailableCount();//总行数
         this.data=contact.getResult();               //显示本页的内容
         this.countMaxPage();
       }
    }
      

  2.   

    楼上的, 那你在JSP中怎么引用PageBean 呢?为什么不直接将result放在PageBean中呢?
      

  3.   

    可以用hibernate的分页。在struts中可以设置相关信息,在jsp中负责显示,在dao实现类中设置当前第几条,显示的条数等。正好符合MVC模式。相关代码你可以网上搜下