求助,用mvc模式实现sql分页

sophiaLin 2010-10-10 12:08:43
每页显示两行,一行有四张图片,第一页显示出来了,关键是点击下一页(图片)内容没有变化
...全文
91 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
syz 2010-10-11
楼上的, 那你在JSP中怎么引用PageBean 呢?为什么不直接将result放在PageBean中呢?
回复
ruru521elva 2010-10-11
貌似也不会 坐坐 沙发 重新安装
回复
qq_992784113 2010-10-11
可以用hibernate的分页。在struts中可以设置相关信息,在jsp中负责显示,在dao实现类中设置当前第几条,显示的条数等。正好符合MVC模式。相关代码你可以网上搜下
回复
East271536394 2010-10-10

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();
}
}

回复
zn85600301 2010-10-10
你的浏览器是什么的 IE5刷新图片有问题
回复
kebin0001 2010-10-10
那就是沒做成功,原因很多,請提供足夠資訊。
回复
cheng87716 2010-10-10
分页 可能 写错了啊
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-10 12:08
社区公告
暂无公告