随笔-6  评论-38  文章-40  trackbacks-0

 

Pager类:

 

package  com.zeroidea.common;

 

public   class  Pager  {

                       
private   int  totalRows;    //  总行数    

                       
private   int  pageSize;     //  单页数据项

                       
private   int  currentPage; //  当前页号

                       
private   int  totalPages;  //  总页数

                       
private   int  startRows;   //  当前页在数据库中记录位置

                       

                       
public  Pager() {  

                       }


                       

                       
public  Pager( int  totalRows) //  指定总行数据

                        
this .setPageSize( 10 );    //  每页 10条数据 

                        
this .setTotalRows(totalRows);   

 

                        
int  pages  =  totalRows / pageSize;

                        
if  ((totalRows % pageSize)  >   0 ) {

                            pages
++ ;

                        }


                        
this .setTotalPages(pages);

                        

                        
this .setStartRows( 0 );

                        
this .setCurrentPage( 1 ); 

                       }


                       

                       
/**

                        * 
@return  the currentPage

                        
*/


                       
public   int  getCurrentPage()  {

                        
return  currentPage;

                       }


                       

                       
/**

                        * 
@param  currentPage the currentPage to set

                        
*/


                       
public   void  setCurrentPage( int  currentPage)  {

                        
this .currentPage  =  currentPage;

                       }


                       

                       
/**

                        * 
@return  the pageSize

                        
*/


                       
public   int  getPageSize()  {

                        
return  pageSize;

                       }


                       

                       
/**

                        * 
@param  pageSize the pageSize to set

                        
*/


                       
public   void  setPageSize( int  pageSize)  {

                        
this .pageSize  =  pageSize;

                       }


                       

                       
/**

                        * 
@return  the startRows

                        
*/


                       
public   int  getStartRows()  {

                        
return  startRows;

                       }


                       

                       
/**

                        * 
@param  startRows the startRows to set

                        
*/


                       
public   void  setStartRows( int  startRows)  {

                        
this .startRows  =  startRows;

                       }


                       

                       
/**

                        * 
@return  the totalPages

                        
*/


                       
public   int  getTotalPages()  {

                        
return  totalPages;

                       }


                       

                       
/**

                        * 
@param  totalPages the totalPages to set

                        
*/


                       
public   void  setTotalPages( int  totalPages)  {

                        
this .totalPages  =  totalPages;

                       }


                       

                       
/**

                        * 
@return  the totalRows

                        
*/


                       
public   int  getTotalRows()  {

                        
return  totalRows;

                       }


                       

                       
/**

                        * 
@param  totalRows the totalRows to set

                        
*/


                       
public   void  setTotalRows( int  totalRows)  {

                        
this .totalRows  =  totalRows;

                       }


                       

                       
/**

                        * 第一页

                        
*/


                       
public   void  fristPage() {

                        
this .setCurrentPage( 1 );

                        
this .setStartRows( 0 );

                       }


                       

                       
/**

                        * 前一页

                        
*/


                       
public   void  previousPage() {

                        
int  nowPage  =   this .getCurrentPage(); 

                        
if  (nowPage  ==   1 ) {

                            
return ;

                        }


                       
this .setCurrentPage( -- nowPage);

                        
this .setStartRows((nowPage  -   1 *   this .getPageSize());

                       }


                       

                       
/**

                        * 后一页

                        
*/


                       
public   void  nextPage() {

                        
int  nowPage  =   this .getCurrentPage();

                        
if  (nowPage  ==   this .getTotalPages()) {

                            
this .lastPage();

                            
return ;

                        }


                        
this .setCurrentPage(nowPage  +   1 );

                        
this .setStartRows(nowPage  *   this .getPageSize());

                       }


                       

                       
/**

                        * 最后一页

                        
*/


                       
public   void  lastPage() {

                        
this .setCurrentPage( this .getTotalPages());

                        
this .setStartRows(( this .getTotalPages()  -   1 *   this .getPageSize());

                       }


                       

                       
/**

                        * 刷新页

                        * 
@param  currentPage

                        
*/


                       
public   void  refreshPage( int  currentPage) {

                        
this .setCurrentPage(currentPage);   

                        
this .setStartRows((currentPage  -   1 *   this .getPageSize());

                        
if  (currentPage  >   this .getTotalPages()) {

                            
this .lastPage();

                        }


                       }


}


 

 

数据查询:

 

                       

                       
/**

                        * 取得总数据行  :有更简单的方法请分享

                        
*/


                       
public   int  selectDataRows()  {

                        
int  rows  =   0 ;

                        List list 
=   null ;

                        Session session 
=   null ;

                        Transaction transaction 
=   null ;

                        Query query 
=   null ;

                        
try   {

                            session 
=  SessionFactory.currentSession();

                            transaction 
=  session.beginTransaction();

                            query 
=  session

                                    .createQuery(
" select msg from Msg as msg " );

                            list 
=  query.list();

                            rows 
=  list.size();

                            transaction.commit();

                            session.close();

                        }
  catch  (Exception excp)  {

                            session.close();

                        }


 

                        
return  rows;

                       }


                       

 

 

 

                       
/**

                        * 分页查询数据

                        * 

                        * 
@param  pager 

                        * 
@return  0 1 0: Success 1: Fail

                        
*/


                       
public  List selectData(Pager pager)  {

                        List list 
=   null ;

                        Session session 
=   null ;

                        Transaction transaction 
=   null ;

                        Query query 
=   null ;

                        

                        
try   {

                            session 
=  SessionFactory.currentSession();

                            transaction 
=  session.beginTransaction();

                            query 
=  session.createQuery( " select msg from Msg as msg order by msg.msgId desc " );

                            query.setFirstResult(pager.getStartRows()); 
//  设置起始行

                            query.setMaxResults(pager.getPageSize());   
//  设置取数据行数

                            list 
=  query.list();

                            transaction.commit();

                            session.close();

                        }
  catch  (Exception excp)  {

                            session.close();

                        }


                        
return  list;

                       }


 

 

 

Action 控制:

 

                       
/**

                        * 查询 方法,,按分页查询

                        * 

                        * 
@param  mapping

                        * 
@param  form

                        * 
@param  request

                        * 
@param  response

                        * 
@return

                        
*/


                       
public   void  selectData(ActionMapping mapping, ActionForm form,

                            HttpServletRequest request, HttpServletResponse response) 
{

                        List list 
=   null ;

                        
int  currentPage  =   1 ;

                        MsgForm msgForm 
=  (MsgForm) form;

                        

                        String pageMethod 
=  request.getParameter( " pageMethod " );          //  存放请求页方法

                        String currentPageTemp 
=  request.getParameter( " currentPage " );    //  存放当前页

                        

                        OperTable operTable 
=   new  OperTable();                           //  操作表类

                        Pager pager 
=   new  Pager(operTable.selectDataRows());             //  页面控制类

                        

                        

                        
if  (currentPageTemp  ==   null ) {

                            pager.setCurrentPage(
1 );

                        }
  else   {

                            currentPage 
=  Integer.parseInt(currentPageTemp);

                        }


                        

                        pager.setCurrentPage(currentPage);

                        

                        
if  (pageMethod  ==   null ) {

                            pager.fristPage();      
//  到首页

                            System.out.println(
" 首页 " );

                        }
else {

                            
if  (pageMethod.equals( " previousPage " )) {

                                pager.previousPage();   
//  到前一页

                                System.out.println(
" 前一页 " );

                            }
  else   if  (pageMethod.equals( " nextPage " )) {

                                pager.nextPage();       
//  到后一页

                                System.out.println(
" 后一页 " );

                            }
  else   if  (pageMethod.equals( " lastPage " )) {

                                pager.lastPage();       
//  到尾页

                                System.out.println(
" 尾页 " );

                            }
else   if  (pageMethod.equals( " refreshPage " )) {

                                pager.refreshPage(currentPage);

                                System.out.println(currentPage);

                                System.out.println(
" 刷新页 " );

                            }
  else   {

                                pager.fristPage();      
//  到首页

                                System.out.println(
" 首页 " );

                            }


                        }


                        

                        request.setAttribute(
" pager " , pager);

                        request.setAttribute(
" msgdata " , list);

                        request.setAttribute(
" info " " 已查询数据 " );

                        msgForm.reset(mapping, request);

                       }


 

 

JSP显示分页: href这样用:

 

                                    
< logic:present name = " pager " >

                                        
< a href = " msg.do?method=select&pageMethod=fristPage&currentPage=<bean:write name='pager' property='currentPage'/> " > 首页 </ a >

                                        
< a href = " msg.do?method=select&pageMethod=previousPage&currentPage=<bean:write name='pager' property='currentPage'/> " > 前一页 </ a >

                                        
< a href = " msg.do?method=select&pageMethod=nextPage&currentPage=<bean:write name='pager' property='currentPage'/> " > 后一页 </ a >

                                        
< a href = " msg.do?method=select&pageMethod=lastPage&currentPage=<bean:write name='pager' property='currentPage'/> " > 尾页 </ a >

                                        共有
< span  class = " font-normal-red " >< bean:write name = " pager "  property = " totalRows " /></ span > 条记录,当前第 < span  class = " font-normal-red " >< bean:write name = " pager "  property = " currentPage " /></ span > 页,共有 < span  class = " font-normal-red " >< bean:write name = " pager "  property = " totalPages " /></ span >  页

                                    
</ logic:present >

 

< html:link >  这样用

 

                  
< logic:present name = " pager " >

                     
< html:link page = " /msg.do?method=select&pageMethod=fristPage "  paramId = " currentPage "  paramName = " pager "  paramProperty = " currentPage " > 首页 </ html:link >

                     
< html:link page = " /msg.do?method=select&pageMethod=previousPage "  paramId = " currentPage "  paramName = " pager "    paramProperty = " currentPage " > 前一页 </ html:link >

                     
< html:link page = " /msg.do?method=select&pageMethod=nextPage "     paramId = " currentPage "  paramName = " pager "    paramProperty = " currentPage " > 下一页 </ html:link >

                     
< html:link page = " /msg.do?method=select&pageMethod=lastPage "     paramId = " currentPage "  paramName = " pager "    paramProperty = " currentPage " > 尾页 </ html:link >

                     共有

                     
< span  class = " font-normal-red " >< bean:write name = " pager "  property = " totalRows "   />

                     
</ span > 条记录,当前第 < span  class = " font-normal-red " >< bean:write name = " pager "  property = " currentPage "   />

                     
</ span > 页,共有 < span  class = " font-normal-red " >< bean:write name = " pager "  property = " totalPages "   />

                     
</ span >  页

                                                                                                 
</ logic:present >
posted on 2006-11-30 16:26 一手的小窝窝 阅读(161) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: