ssh2分页的问题
在点下一页之后没有数据了
pageBean页面:
private List list;//要返回的某一页的记录列表
private int allRow; //总记录数
private int totalPage;//总页数
private int currentPage;//当前页
private int pageSize;//每页记录数
private boolean isFirstPage;//是否为第一页
private boolean isLastPage;//是否为最后一页
private boolean hasPreviousPage;//是否有前一页
private boolean hasNextPage;//是否有下一页
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getAllRow() {
return allRow;
}
public void setAllRow(int allRow) {
this.allRow = allRow;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}
public void setLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/** *//**
* 初始化分页信息
*/
public void init(){
this.isFirstPage = isFirstPage;
this.isLastPage = isLastPage;
this.hasPreviousPage =hasPreviousPage;
this.hasNextPage = hasNextPage;
}
/** *//**
* 计算总页数,静态方法,供外部直接通过类名调用
* @param pageSize 每页记录数
* @param allRow 总记录数
* @return 总页数
*/
public static int countTotalPage(final int pageSize,final int allRow){
int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;
return totalPage;
}
/** *//**
* 计算当前页开始记录
* @param pageSize 每页记录数
* @param currentPage 当前第几页
* @return 当前页开始记录号
*/
public static int countOffset(final int pageSize,final int currentPage){
final int offset = pageSize*(currentPage-1);
return offset;
}
/** *//**
* 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
* @param page 传入的参数(可能为空,即0,则返回1)
* @return 当前页
*/
public static int countCurrentPage(int page){
final int curPage = (page==0?1:page);
return curPage;
}
实现接口dao
public int countPage(String hql) {
return getHibernateTemplate().find(hql).size();
}
public List queryForPage(final String hql, final int offset, final int length) {
List list=getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
Query q=session.createQuery(hql);
q.setFirstResult(offset);
q.setMaxResults(length);
List list=q.list();
return list;
}
});
return list;
}
serviceImpl
public PageBean queryforPage(int pagesize, int page) {
final String hql = "from Users";//查询语句
int allRow = udao.countPage(hql);//总记录数
int totalPage = PageBean.countTotalPage(pagesize, allRow);//总页数
final int offset = PageBean.countOffset(pagesize, page);//当前页开始记录
final int length = pagesize;//每页记录数
final int currentPage = PageBean.countCurrentPage(page);
List<Users> list = udao.queryForPage(hql,offset, length);//"一页"的记录
//把分页信息保存到Bean中
PageBean pageBean = new PageBean();
pageBean.setPageSize(pagesize);
pageBean.setCurrentPage(currentPage);
pageBean.setAllRow(allRow);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.init();
System.out.println(totalPage);
return pageBean;
}
action
public String login() {
u.setName(uname);
u.setPwd(upwd);
if (uservice.login(u)==true) {
this.pageBean=uservice.queryforPage(2, page);
request.setAttribute("list", pageBean.getList());
return "list";
}
return "error";
}
//@Override
//public String execute() throws Exception {
////分页的pageBean,参数pageSize表示每页显示记录数,page为当前页
//this.pageBean = uservice.queryforPage(2, page);
//return "list";
//}
public String qpage(){
this.pageBean=uservice.queryforPage(2, page);
return "list";
}
页面
<table border="1">
<tr><td>用户ID</td><td>用户名称</td><td>用户密码</td><td colspan="3" align="center">操作</td></tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.UId }</td>
<td>${user.name}</td>
<td>${user.pwd}</td>
<td><a href="save.jsp">添加</a></td>
<td><a href="user!findByid?id=${user.UId}">修改</a></td>
<td><a href="user!delete?id=${user.UId}">删除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="6">
共<s:property value="pageBean.allRow"/> 条记录
共<s:property value="pageBean.totalPage"/> 页
当前第<s:property value="pageBean.currentPage"/>页<br/>
<s:if test="%{pageBean.currentPage == 1}">
第一页 上一页
</s:if>
<s:else>
<a href="user!qpage?page=1">第一页</a>
<a href="user!qpage?page=<s:property value="%{pageBean.currentPage-1}"/>">上一页</a>
</s:else>
<s:if test="%{pageBean.currentPage != pageBean.totalPage}">
<a href="user!qpage?page=<s:property value="%{pageBean.currentPage+1}"/>">下一页</a>
<a href="user!qpage?page=<s:property value="pageBean.totalPage"/>">最后一页</a>
</s:if>
<s:else>
下一页 最后一页
</s:else>
</td>
</tr>
[解决办法]
看看这段代码能不能帮你
- Java code
String sql="select * from t_board"; List <Message>list=db.QueryExecute1(sql); int countrecord=list.size(); int pagerecord=3; int countpage=0; if(countrecord%pagerecord==0){ countpage=countrecord/pagerecord; } else{ countpage=countrecord/pagerecord+1; } int currentpage=1; String pagetemp=request.getParameter("currentpage"); if(pagetemp!=null){ currentpage=Integer.parseInt(pagetemp); } if(currentpage-1<=0){ currentpage=1; } if(currentpage+1>countpage){ currentpage=countpage; } Message message=null; int count=0; for(int i=countrecord-1-(pagerecord*(currentpage-1));i>=0;i--){ message=list.get(i); %> 共有<%=countrecord %>条记录 当前第<%=currentpage %>页 <%if(currentpage==1){ %> 首页| 上一页 <% }else{%> <a href="messageBook.jsp?currentpage=1">首页</a>| <a href="messageBook.jsp?currentpage=<%=currentpage-1 %>">上一页</a><%} %>| <%if(currentpage==countpage) {%> 下一页| 尾页 <%}else{ %> <a href="messageBook.jsp?currentpage=<%=currentpage+1 %>"> 下一页</a>|<a href="messageBook.jsp?currentpage=<%=countpage %>">尾页</a><%} %>