论坛首页 入门技术论坛

strut2中分页的实现的初体验

浏览 3438 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (2)
作者 正文
   发表时间:2010-04-25  
实现构想:把每一页封装成一个对象(PagerBean),点击“下一页”、“上一页”、“首页”、“末页”传递不同的参数值,返回一PagerBean对象。
1、 PagerBean.java
属性:
private int pageSize;每页5条数据
private int currentPage = 1; // 当前页
private int totalPages = 0; // 总页数
private int totalRows = 0; // 总数据数
private int pageStartRow = 0;// 每页的起始数
private int pageEndRow = 0; // 每页显示数据的终止数
private int startRow=0; //当前页在数据库中的起始行
private boolean hasNextPage = false; // 是否有下一页
private boolean hasPreviousPage = false; // 是否有前一页
方法:
public PagerBean();
public PagerBean(int totalRows,int pageSize);
public PagerBean(int currentPage,int startRow,int pageEndRow,int pageStartRow,int totalRows,int pageSize,int totalPages);
public void first();//首页
public void previous();//上一页
public void next();//下一页
public void last();//末页
public int getCurrentPage() ;
public void setCurrentPage(int currentPage) ;
public int getTotalPages() ;
public void setTotalPages(int totalPages);
public int getPageSize();
public void setPageSize(int pageSize) ;
public int getTotalRows() ;
public void setTotalRows(int totalRows);
public int getPageStartRow();
public void setPageStartRow(int pageStartRow);
public int getPageEndRow();
public void setPageEndRow(int pageEndRow) ;
public boolean isHasNextPage();
public void setHasNextPage(boolean hasNextPage) ;
public boolean isHasPreviousPage();
public void setHasPreviousPage(boolean hasPreviousPage);
public int getStartRow() ;
public void setStartRow(int startRow) ;

2、 界面代码:
<td colspan="3" align="center">
    第<s:property value="thispage.getCurrentPage()" />页/
    共<s:property value="thispage.getTotalPages()" />页
     <s:url id="url_first" value="showbooks.action">
     <s:param name="currentPage" value="1"></s:param>
     </s:url>
     <s:url id="url_pre" value="showbooks.action">
         <s:param name="currentPage" value="2"></s:param>
     </s:url>
     <s:url id="url_next" value="showbooks.action">
         <s:param name="currentPage" value="3"></s:param>
     </s:url>
     <s:url id="url_last" value="showbooks.action">
         <s:param name="currentPage" value="4"></s:param>
     </s:url>
     <s:a href="%{url_first}">首页</s:a>
     <s:a href="%{url_pre}">上一页</s:a>
     <s:a href="%{url_next}">下一页</s:a>
     <s:a href="%{url_last}">末页</s:a>
    </td>
3、 Action代码:
//实现分页
public List<Book> getShowPage(){
HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
HttpSession session=request.getSession();
List<Book> booklist=null;
PagerBLO pb=new PagerBLO();
BookBLO bb=new BookBLO();
totalRows=bb.getTotal_BLO();
if(session.getAttribute("pB")==null){
pB=new PagerBean(totalRows,pageSize);//pram1:总记录数;pram2:每页记录数
session.setAttribute("pB", pB);
}
else{
pB=(PagerBean) session.getAttribute("pB");
}
if(currentPage==1){
pB.first();
}
if(currentPage==2){
pB.previous();
}
if(currentPage==3){
pB.next();
}
if(currentPage==4){
pB.last();
}
booklist=pb.getBooksByPager(pB);
return booklist;
}
4、 Server相关方法:
public List<Book> getBooksByPager(PagerBean page){
List<Book> books=null;
PagerDAO pd=new PagerDAO();
books=pd.getAnyBook(page.getPageSize(), page.getStartRow());
return books;
}
5、 Dao层代码
public List<Book> getAnyBook(int pageSize,int startRow){
List<Book> anyBooks = new ArrayList();
Session session = HibernateSessionFactory.getSession();
String hql = "from Book";
Query q=session.createQuery(hql);
q.setFirstResult(startRow);
q.setMaxResults(pageSize);
anyBooks = q.list();
return anyBooks;
}

   发表时间:2010-04-27  
哥们,用下编辑器的code功能吧,你这样视觉太不爽了
0 请登录后投票
   发表时间:2010-04-28  
mxl86 写道
哥们,用下编辑器的code功能吧,你这样视觉太不爽了

说的也是,谢谢!下次一定用!
0 请登录后投票
   发表时间:2010-07-18  
1、 PagerBean.java 
属性: 
private int pageSize;每页5条数据 
private int currentPage = 1; // 当前页 
private int totalPages = 0; // 总页数 
private int totalRows = 0; // 总数据数 
private int pageStartRow = 0;// 每页的起始数 
private int pageEndRow = 0; // 每页显示数据的终止数 
private int startRow=0; //当前页在数据库中的起始行 
private boolean hasNextPage = false; // 是否有下一页 
private boolean hasPreviousPage = false; // 是否有前一页 
方法: 
public PagerBean(); 
public PagerBean(int totalRows,int pageSize); 
public PagerBean(int currentPage,int startRow,int pageEndRow,int pageStartRow,int totalRows,int pageSize,int totalPages); 
public void first();//首页 
public void previous();//上一页 
public void next();//下一页 
public void last();//末页 
public int getCurrentPage() ; 
public void setCurrentPage(int currentPage) ; 
public int getTotalPages() ; 
public void setTotalPages(int totalPages); 
public int getPageSize(); 
public void setPageSize(int pageSize) ; 
public int getTotalRows() ; 
public void setTotalRows(int totalRows); 
public int getPageStartRow(); 
public void setPageStartRow(int pageStartRow); 
public int getPageEndRow(); 
public void setPageEndRow(int pageEndRow) ; 
public boolean isHasNextPage(); 
public void setHasNextPage(boolean hasNextPage) ; 
public boolean isHasPreviousPage(); 
public void setHasPreviousPage(boolean hasPreviousPage); 
public int getStartRow() ; 
public void setStartRow(int startRow) ; 

2、 界面代码: 
<td colspan="3" align="center"> 
    第<s:property value="thispage.getCurrentPage()" />页/ 
    共<s:property value="thispage.getTotalPages()" />页 
     <s:url id="url_first" value="showbooks.action"> 
     <s:param name="currentPage" value="1"></s:param> 
     </s:url> 
     <s:url id="url_pre" value="showbooks.action"> 
         <s:param name="currentPage" value="2"></s:param> 
     </s:url> 
     <s:url id="url_next" value="showbooks.action"> 
         <s:param name="currentPage" value="3"></s:param> 
     </s:url> 
     <s:url id="url_last" value="showbooks.action"> 
         <s:param name="currentPage" value="4"></s:param> 
     </s:url> 
     <s:a href="%{url_first}">首页</s:a> 
     <s:a href="%{url_pre}">上一页</s:a> 
     <s:a href="%{url_next}">下一页</s:a> 
     <s:a href="%{url_last}">末页</s:a> 
    </td> 
3、 Action代码: 
//实现分页 
public List<Book> getShowPage(){ 
HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST); 
HttpSession session=request.getSession(); 
List<Book> booklist=null; 
PagerBLO pb=new PagerBLO(); 
BookBLO bb=new BookBLO(); 
totalRows=bb.getTotal_BLO(); 
if(session.getAttribute("pB")==null){ 
pB=new PagerBean(totalRows,pageSize);//pram1:总记录数;pram2:每页记录数 
session.setAttribute("pB", pB); 
} 
else{ 
pB=(PagerBean) session.getAttribute("pB"); 
} 
if(currentPage==1){ 
pB.first(); 
} 
if(currentPage==2){ 
pB.previous(); 
} 
if(currentPage==3){ 
pB.next(); 
} 
if(currentPage==4){ 
pB.last(); 
} 
booklist=pb.getBooksByPager(pB); 
return booklist; 
} 
4、 Server相关方法: 
public List<Book> getBooksByPager(PagerBean page){ 
List<Book> books=null; 
PagerDAO pd=new PagerDAO(); 
books=pd.getAnyBook(page.getPageSize(), page.getStartRow()); 
return books; 
} 
5、 Dao层代码 
public List<Book> getAnyBook(int pageSize,int startRow){ 
List<Book> anyBooks = new ArrayList(); 
Session session = HibernateSessionFactory.getSession(); 
String hql = "from Book"; 
Query q=session.createQuery(hql); 
q.setFirstResult(startRow); 
q.setMaxResults(pageSize); 
anyBooks = q.list(); 
return anyBooks; 
} 
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics