一种struts分页方法

分页处理的bean类PageableResultSet.java的构造函数参数为一个ResultSet对象,即执行查询语句得到的ResultSet。其具体代码如下:
public   class  PageableResultSet  extends ResultSet {

    
protected java.sql.ResultSet rs = null;

    
protected int rowsCount;

    
protected int pageSize;

    
protected int curPage;

    
protected String command = "";

    
public PageableResultSet(java.sql.ResultSet rs)
            
throws java.sql.SQLException {
        
if (rs == null)
            
throw new SQLException("given ResultSet is NULL""user");

        rs.last();
//将指针移动到此 ResultSet 对象的最后一行
        rowsCount = rs.getRow();//检索当前行编号
        rs.beforeFirst();//将指针移动到此 ResultSet 对象的开头,正好位于第一行之前

        
this.rs = rs;
    }


    
/**返回当前页号
    
*/

    
public int getCurPage() {
        
return curPage;
    }


    
/**返回总页数
    
*/

    
public int getPageCount() {
        
if (rowsCount == 0)
            
return 0;
        
if (pageSize == 0)
            
return 1;
        
// calculate PageCount
        double tmpD = (double) rowsCount / pageSize;
        
int tmpI = (int) tmpD;
        
if (tmpD > tmpI)
            tmpI
++;
        
return tmpI;
    }


    
/**返回当前页的记录条数
    
*/

    
public int getPageRowsCount() {
        
if (pageSize == 0)
            
return rowsCount;
        
if (getRowsCount() == 0)
            
return 0;
        
if (curPage != getPageCount())
            
return pageSize;
        
return rowsCount - (getPageCount() - 1* pageSize;

    }


    
/**返回分页大小
    
*/

    
public int getPageSize() {
        
return pageSize;
    }


    
/**返回总记录行数
    
*/

    
public int getRowsCount() {
        
return rowsCount;
    }


    
/**转到指定页
    
*/

    
public void gotoPage(int page) {
        
if (rs == null)
            
return;
        
if (page < 1)
            page 
= 1;
        
if (page > getPageCount())
            page 
= getPageCount();
        
int row = (page - 1* pageSize + 1;
        
try {
            rs.absolute(row);
//将指针移动到此 ResultSet 对象的给定行编号
            curPage = page;
        }
 catch (java.sql.SQLException e) {
        }

    }


    
/**
    * 转到当前页的第一条记录
    * 
@exception java.sql.SQLException 异常说明。
    
*/

    
public void pageFirst() throws SQLException {
        
int row = (curPage - 1* pageSize + 1;
        rs.absolute(row);
    }


    
/**
    * 转到当前页的最后一条记录
    * 
@exception java.sql.SQLException 异常说明。
    
*/

    
public void pageLast() throws SQLException {
        
int row = (curPage - 1* pageSize + getPageRowsCount();
        rs.absolute(row);
    }


    
/**设置分页大小
    
*/

    
public void setPageSize(int pageSize) {
        
if (pageSize >= 0{
            
this.pageSize = pageSize;
            curPage 
= 1;
        }

    }

    public boolean next() throws SQLException {
        // TODO Auto-generated method stub
        return rs.next();
    }

}

分页action类PageAction.java的关键代码如下:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/

package  com.lyt.struts.action;

import  javax.servlet.ServletContext;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;
import  javax.sql.DataSource;

import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

import  bean. * ;

import  common. * ;

import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.util. * ;
import  dao.ApplicationDAO;

/** 
 * MyEclipse Struts
 * Creation date: 07-14-2007
 * 
 * XDoclet definition:
 * @struts.action validate="true"
 
*/

public   class PageAction  extends  Action  {
    
/*
     * Generated Methods
     
*/


    
/** 
     * Method execute
     * 
@param mapping
     * 
@param form
     * 
@param request
     * 
@param response
     * 
@return ActionForward
     
*/

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        
        ServletContext context 
= servlet.getServletContext();
        
//从配置文件中得到数据源
        DataSource dataSource = (DataSource) context.getAttribute("db");
        DB db 
= new DB(dataSource);
                HttpSession session
=request.getSession();
        
try{
        request.setCharacterEncoding(
"gb2312");
        }
catch(Exception e){
            e.printStackTrace();
        }

        UserBean user
=(UserBean)session.getAttribute("usr");
        
if(user==null){
            
return mapping.findForward("invalidSession");
        }

        
int staffId=user.getID();
        String sappType
=request.getParameter("appType");
        
        
            
int appType=Integer.valueOf(sappType).intValue();
            ResultSet rs
=ApplicationDAO.getBjsxRS(db, staffId, appType);
        
        
int pageNumber=1;
        
try {
            pageNumber
=Integer.parseInt(request.getParameter("pageNumber"));
        }
 catch (Exception e) {
            pageNumber
=1;
        }

        PageableResultSet prs
=null;
        
try{
        prs
=new PageableResultSet(rs);
        }
catch(Exception e){
            e.printStackTrace();
        }

        prs.setPageSize(
10);
          prs.gotoPage(pageNumber);
          
          ArrayList
<Application> list =new ArrayList<Application>();
        
try {
            
for(int i=0; i<prs.getPageRowsCount(); i++{
                Application app
=new Application();
                
                list.add(app);

                prs.next();
            }

        }
 catch (SQLException e) {
            e.printStackTrace();
        }

        request.setAttribute(
"totalPage",prs.getPageCount());
        request.setAttribute(
"currentPage",prs.getCurPage());
        
if(list.size()>0){
        request.setAttribute(
"list", list);
        }

        db.close();
        
        
return mapping.findForward("page");
    }

}

显示分页的page.jsp代码如下:
<% @ page language = " java "  pageEncoding = " gb2312 "
    contentType
= " text/html;charset=gb2312 " %>
<% @ page  import = " bean.*,common.* " %>

<% @ taglib uri = " /WEB-INF/struts-html.tld "  prefix = " html " %>
<% @ taglib uri = " /WEB-INF/struts-bean.tld "  prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-logic.tld "  prefix = " logic " %>
<% @ taglib uri = " /WEB-INF/struts-nested.tld "  prefix = " nested " %>


<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
    
< head >

        
< title > 主页 </ title >
        
< meta http - equiv = " Content-Type "  content = " text/html; charset=GB2312 " >

        
< style type = " text/css " >
<!--
.style1 
{color: #F4F4F4}
-->
</ style >
    
</ head >

    
< body >
        
<%
        
int  currentPage  =   1 , totalPage  =   0 ;
        String sappType
= (String)request.getAttribute( " appType " );
        
try   {
            currentPage 
= (Integer)request.getAttribute("currentPage");
        }
  catch  (Exception e)  {}
        
try   {
            totalPage 
=(Integer)request.getAttribute("totalPage");
        }
  catch  (Exception e)  {}
        
%>

        
< table >
            
< caption >
            
            
</ caption >
            
< tr >
            
            
</ tr >
            
< logic:present name = " list "  scope = " request " >
            
< logic:iterate id = " shixiang "  name = " list " >
            
< tr >
            
            
</ tr >
            
</ logic:iterate >
          
</ logic:present >
        
</ table >

        
< center >
        
< p >
            第
            
<%= currentPage  %>
            页,共
            
<%= totalPage  %>
            页 
& nbsp; & nbsp;
            
< a href =/ sky_6yt / page. do ? pageNumber = 1 && appType =<%= sappType  %>> 首页 </ a >
            
<% if (currentPage <= 1 ) {%>
            上一页
            
<%}
else {%>
            
<a href=/sky_6yt/page.do?pageNumber=<%=currentPage-1 %>&&appType=<%=sappType %>>上一页</a>
            
<%}
%>
            
<% if (currentPage == totalPage) {%>
            下一页
            
<%}
else {%>
            
<a href=/sky_6yt/page.do?pageNumber=<%=currentPage+1 %>&&appType=<%=sappType %>>下一页</a>
            
<%}
  %>
            
< a href =/ sky_6yt / page. do ? pageNumber =<%= totalPage  %>&& appType =<%= sappType  %>> 末页 </ a >
        
</ p >
        
</ center >
    
</ body >
</ html >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值