struts里的分页技术。。。(还不是通用的,有点hard_code)

类别:Java 点击:0 评论:0 推荐:

思路:
一般在list页面才要分页
     1)所以加入一个参数来判断是否分页,如pageControll=true(当然可以不用,这步)
     2)建一个转用的actionform(当然页可以是普通bean,我这里只是为了页面上少写scriplet)  --网上找了个改造成自己想
        要的 --PageController.java(见附)
    3)在处理list的action代码里加入判断是否执行 PageController.java 里面的main()方法 如:
      pageControll = request.getParameter("pageControll");
  //In case the parameter is null
  currentPage = (request.getParameter("currentPage") == null)?(new String("0")):request.getParameter("currentPage");

  //change the String type to int
  cPage = Integer.parseInt( currentPage );

  //first get the borad and then get it's boardItems
  boardItemsList = boardForm.getBoardItems();
  //fen ye
  if( pageControll != null && pageControll.equals("true") ) {
   PageController pg = (new PageController()).main(boardItemsList,cPage);
   request.setAttribute("PageControllerForm",pg);
  }

在jsp页面上写:
<bean:define id="offset" name="PageControllerForm" property="pageStartRow" type="Integer"/>
<bean:define id="size" name="PageControllerForm" property="pageSize" type="Integer"/>

<bean:define id="previousPage" name="PageControllerForm" property="previousPage" type="Integer"/>
<bean:define id="nextPage" name="PageControllerForm" property="nextPage" type="Integer"/>
<bean:define id="totalPages" name="PageControllerForm" property="totalPages" type="Integer"/>

<% String url = "/do/BoardActions?method=listItems&pageControll=true&currentPage=";%>
 
 <logic:equal name="PageControllerForm" property="hasPrevious" value="true">
 <html:link page="<%=url+1%>" paramId="id" paramName="BoardForm" paramProperty="id">
  <bean:message key="common.fenye.first"/>
 </html:link>
 <html:link page="<%=url+previousPage%>" paramId="id" paramName="BoardForm" paramProperty="id">
  <bean:message key="common.fenye.previous"/>
 </html:link>
 </logic:equal>

 <logic:equal name="PageControllerForm" property="hasNext" value="true">
 <html:link page="<%=url+nextPage%>" paramId="id" paramName="BoardForm" paramProperty="id">
  <bean:message key="common.fenye.next"/>
 </html:link>
 <html:link page="<%=url+totalPages%>" paramId="id" paramName="BoardForm" paramProperty="id">
  <bean:message key="common.fenye.last"/>
 </html:link>
 </logic:equal>

 <logic:iterate id="boarditems" name="BoardItemsList" offset="<%=offset.toString()%>" length="<%=size.toString()%>">


附:
package com.lpeng.base;

import java.util.List;
import org.apache.struts.action.ActionForm;

public class PageController extends ActionForm {

    int totalRowsAmount; //总行数
    boolean rowsAmountSet; //是否设置过totalRowsAmount 1
    int pageSize=10;   //每页行数
    int currentPage=1; //当前页码
    int nextPage;
    int previousPage;
    int totalPages;  //总页数
    boolean hasNext;  //是否有下一页
    boolean hasPrevious; //是否有前一页
    //String description;
    int pageStartRow; //开始行
    int pageEndRow;

   
    public PageController(int totalRows,int cPage){
  setTotalRowsAmount(totalRows,cPage);
    }
    public PageController(){}

 public int getTotalRowsAmount() {
  return totalRowsAmount;
 }
    public void setTotalRowsAmount(int i,int j) {
  if(!this.rowsAmountSet){
  totalRowsAmount = i;
  totalPages=totalRowsAmount/pageSize+1;
  setCurrentPage((j == 0)?totalPages:j);
  this.rowsAmountSet=true;
  }
 }
 
  public boolean isRowsAmountSet() {
  return rowsAmountSet;
  }
  public void setRowsAmountSet(boolean b) {
  rowsAmountSet = b;
  }

  public void setCurrentPage(int i) {
   currentPage = i;
   nextPage=currentPage+1;
   previousPage=currentPage-1;
   //计算当前页开始行和结束行
   if(currentPage*pageSize<totalRowsAmount){
    pageEndRow=currentPage*pageSize;
    pageStartRow=pageEndRow-pageSize;
   
   }else{//for the last page,这里我是为了默认的那页(最后一页)不足pagesize时也能显示pagesize这么多。。。。。。。
    pageEndRow=totalRowsAmount;   
    pageStartRow=pageEndRow-pageSize;
   }
   if (nextPage>totalPages){
    hasNext=false;
   }else{
    hasNext=true;
   }
   if(previousPage==0){
   hasPrevious=false;
   }else{
    hasPrevious=true;
   }
  }

  public int getCurrentPage() {
   return currentPage;
  }

  public boolean isHasNext() {
   return hasNext;
  }
  public void setHasNext(boolean b) {
   hasNext = b;
  }

  public boolean isHasPrevious() {
   return hasPrevious;
  }
  public void setHasPrevious(boolean b) {
   hasPrevious = b;
  }

  public int getNextPage() {
   return nextPage;
  }
  public void setNextPage(int i) {
   nextPage = i;
  }

  public int getPageSize() {
   return pageSize;
  }
  public void setPageSize(int i) {
   pageSize = i;
  }

  public int getPreviousPage() {
   return previousPage;
  }
  public void setPreviousPage(int i) {
   previousPage = i;
  }

  public int getTotalPages() {
   return totalPages;
  }
  public void setTotalPages(int i) {
   totalPages = i;
  }

  public int getPageEndRow() {
   return pageEndRow;
  }
  public void setPageEndRow( int pageEndRow ) {
   this.pageEndRow = pageEndRow;
  }

  public int getPageStartRow() {
   return pageStartRow;
  }
  public void setPageStartRow( int pageStartRow) {
   this.pageStartRow = pageStartRow;
  }
 
 
  public PageController main(List list,int currentPage){
   int rows = list.size();
   PageController pc=new PageController(rows,currentPage);
   return pc;
  }
}

本文地址:http://com.8s8s.com/it/it14587.htm