小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Webwork+hibernate+spring分頁(yè)實(shí)現(xiàn)

 whiskey1122 2009-03-19

 在實(shí)際的開(kāi)發(fā)中,學(xué)習(xí)了其他人的分頁(yè)方案,經(jīng)過(guò)修改形成了一個(gè)可以直接使用的分頁(yè)方案,現(xiàn)說(shuō)明如下。 該分頁(yè)可以實(shí)現(xiàn)定制每頁(yè)顯示的記錄數(shù)、上下翻頁(yè)和跳到一個(gè)指定的頁(yè)面的功能。

一、定義了一個(gè)Page類(lèi)和PageHelper類(lèi)。

Page.java

public class Page {

 //-----------------數(shù)據(jù)庫(kù)相關(guān)參數(shù)-----------------

 //默認(rèn)每頁(yè)顯示記錄數(shù)?

 private int perPage = 10;

 //頁(yè)碼

 private int page = 1;

 //記錄總數(shù)

 private int total = 0;

 //開(kāi)始記錄數(shù)(新的游標(biāo)起始位置)

 private int startRs=0;

 //翻頁(yè)的url(路徑),不帶參數(shù),參數(shù)由其它屬性指定

 private String url = "";

 //------------------頁(yè)面相關(guān)參數(shù)------------------

 //首頁(yè)

 private String firstPage = "";

 //上一頁(yè)

 private String prePage = "";

 //下一頁(yè)

 private String nextPage = "";

 //最后一頁(yè)

 private String lastPage = "";

 //每頁(yè)顯示記錄數(shù)

 private String numPage = "";

 //跳轉(zhuǎn)到第幾頁(yè)

 private String jumpPage = "";

 //總頁(yè)數(shù)

 private int totalPage = 1;

 //

 private String hql = "";

 

 public Page() {

 }

//省略getXXX()和setXXX()

PageHelper.java

public class PageHelper {

    private boolean isHaveParam = false;//是否有參數(shù)?

    

 public PageHelper() {

     //

 }

 /**

  * 用于計(jì)算用戶(hù)自定義每頁(yè)顯示記錄數(shù)

  *

  * @return

  */

 public int doCustomPerPage(int customPerPage) {

  int perPage = customPerPage;

  String cPerPage = String.valueOf(perPage);

  if (!"".equals(cPerPage)) {

      ActionContext.getContext().getSession().put(Constant.COUNT_PERPAGE,cPerPage);

  }

  String cUserPerPage =

      (String) ActionContext.getContext().getSession().get(Constant.COUNT_PERPAGE);

  if (null != cUserPerPage) {

   perPage = Integer.parseInt(cUserPerPage);

  }

  return perPage;

 }

 public Page getNextPageObject(Page pageObject) {

  int page = 1;

  int total = 0;

  int perPage = 20;

  // 取分頁(yè)器參數(shù)

  

  //頁(yè)碼

  page = pageObject.getPage();

  //總記錄數(shù)

  total = pageObject.getTotal();

  //每頁(yè)顯示的記錄數(shù)

  perPage = pageObject.getPerPage();

  //需要分頁(yè)的action名稱(chēng)

  String url = pageObject.getUrl();

  

  boolean display1 = false;//有首頁(yè)?

  boolean display2 = false;//有上一頁(yè)?

  boolean display3 = false;//有下一頁(yè)?

  boolean display4 = false;//有最后一頁(yè)?

  //總頁(yè)數(shù)

  int totalPage = (total + perPage - 1) / perPage;

  if (page > totalPage || page < 0) {

   page = 1;

  }

  // 首頁(yè)

  StringBuffer firstPageBuffer = new StringBuffer();

  if (totalPage > 1) {

      if(isHaveParam){

          firstPageBuffer.append(url + "&pageNo=1");

      }else{

          firstPageBuffer.append(url + "?pageNo=1");

      }

   display1 = true;

  }

    

  // 上一頁(yè)?

  StringBuffer prePageBuffer = new StringBuffer();

  if (page > 1) {

      if(isHaveParam){

          prePageBuffer.append(url + "&pageNo=" + (page - 1));

      }else{

          prePageBuffer.append(url + "?pageNo=" + (page - 1));

      }

   display2 = true;

  }

  

  // 下一頁(yè)?

  StringBuffer nextPageBuffer = new StringBuffer();

  if (page < totalPage) {

      if(isHaveParam){

          nextPageBuffer.append(url + "&pageNo=" + (page + 1));

      }else{

          nextPageBuffer.append(url + "?pageNo=" + (page + 1));

      }

   display3 = true;

  }

  

  // 最后一頁(yè)?

  StringBuffer lastPageBuffer = new StringBuffer();

  if (totalPage > 1) {

   if(isHaveParam){

       lastPageBuffer.append(url + "&pageNo=" + totalPage);

      }else{

          lastPageBuffer.append(url + "?pageNo=" + totalPage);

      }   

   display4 = true;

  }

  

  // 每頁(yè)顯示記錄數(shù)?

  StringBuffer numPageBuffer = new StringBuffer();

  numPageBuffer.append("<select name=\"perPage\" ");

  numPageBuffer.append(" onchange=\"");

  numPageBuffer.append(" if(this.options[this.selectedIndex]");

  numPageBuffer.append(".value!=''){");

  if(isHaveParam){

      numPageBuffer.append(" location='" + url + "&");

     }else{

         numPageBuffer.append(" location='" + url + "?");

     }

  numPageBuffer.append("pageNo=1");

  numPageBuffer.append("&perPageCount='+this.options[this.selectedIndex]");

  numPageBuffer.append(".value;}\">");

  for (int i = 1; i <= 4; i++) {

   numPageBuffer.append("<option value=\"" + (i * 10) + "\"");

   if ((i * 10) == perPage) {

    numPageBuffer.append(" selected>" + (i * 10) + "</option>");

   } else {

    numPageBuffer.append(">" + (i * 10) + "</option>");

   }

  }

  numPageBuffer.append("</select>");

  pageObject.setNumPage(numPageBuffer.toString());

  // 跳轉(zhuǎn)到第幾頁(yè)

  StringBuffer jumpPageBuffer = new StringBuffer();

  jumpPageBuffer.append("<select name=\"pages\"");

  jumpPageBuffer.append(" onchange=\"");

  jumpPageBuffer.append(" if(this.options[this.selectedIndex]");

  if(isHaveParam){

      jumpPageBuffer.append(".value!=''){location='" + url + "&");

     }else{

         jumpPageBuffer.append(".value!=''){location='" + url + "?");

     }

  jumpPageBuffer.append("pageNo='+this.options[this.selectedIndex]");

  jumpPageBuffer.append(".value;}\">");

  for (int i = 1; i <= totalPage; i++) {

   jumpPageBuffer.append("<option value=\"" + i + "\"");

   if (i == page) {

    jumpPageBuffer.append("selected");

   }

   jumpPageBuffer.append(">" + i + "</option>");

  }

  jumpPageBuffer.append("</select>");

  pageObject.setJumpPage(jumpPageBuffer.toString());

  

  pageObject.setTotal(total);

  pageObject.setPage(page);

  pageObject.setTotalPage(totalPage);

  if (display1) {     

      pageObject.setFirstPage("<A HREF=\""+firstPageBuffer.toString()+"\">首 頁(yè)</A>");

  }else{

      pageObject.setFirstPage("首 頁(yè)");

  }

  if (display2) {

      pageObject.setPrePage("<A HREF=\""+prePageBuffer.toString()+"\">上一頁(yè)</A>");

  }else{

      pageObject.setPrePage("上一頁(yè)");

  }

  if (display3) {

      pageObject.setNextPage("<A HREF=\""+nextPageBuffer.toString()+"\">下一頁(yè)</A>");

  }else{

      pageObject.setNextPage("下一頁(yè)");

  }

  if (display4) {

      pageObject.setLastPage("<A HREF=\""+lastPageBuffer.toString()+"\">尾 頁(yè)</A>");

  }else{

      pageObject.setLastPage("尾 頁(yè)");

  }

  

  pageObject.setNumPage(numPageBuffer.toString());

  pageObject.setJumpPage(jumpPageBuffer.toString());

  

  return pageObject;

 }

    /**

     * @return Returns the isHaveParam.

     */

    public boolean isHaveParam() {

        return isHaveParam;

    }

    /**

     * @param isHaveParam The isHaveParam to set.

     */

    public void setHaveParam(boolean isHaveParam) {

        this.isHaveParam = isHaveParam;

    }

}

      Page類(lèi)主要是存儲(chǔ)分頁(yè)時(shí)每頁(yè)的相關(guān)信息,PageHelper類(lèi)主要是根據(jù)用戶(hù)通過(guò)URL送來(lái)的參數(shù)設(shè)置Page對(duì)象的相關(guān)屬性,然后再用這些屬性設(shè)置頁(yè)面中的分頁(yè)。

二、頁(yè)面中的代碼

<!--

<table width="20%" align="center"><tr><td height="5"></td></tr></table>

<table width="75%" align=center cellpadding="0" cellspacing="0" bgcolor="#F4F4F4" style="border: 1 solid #CCCCCC">

<thead>

<tr>

<td height="30">   <font color=blue><ww:property value="page.total"/></font> 條記錄 

<ww:property value="page.firstPage"/> 

<ww:property value="page.prePage"/> 

<ww:property value="page.nextPage"/> 

<ww:property value="page.lastPage"/>   

頁(yè)次:<ww:property value="page.page"/>/<ww:property value="page.totalPage"/>  

每頁(yè)顯示 <ww:property value="page.numPage"/>   

跳轉(zhuǎn)到第 <ww:property value="page.jumpPage"/> 頁(yè)</td>

</tr>

</thead>

</table>

-->

主要是把page對(duì)象的屬性通過(guò)Webwork中定義的標(biāo)簽顯示在頁(yè)面中。

三、Action中的調(diào)用代碼

public String execute() throws Exception{

       

        if (logger.isDebugEnabled()) {

            logger.debug("teamId="+teamId+"--載入日志成功!");

        }

       

              user = (User)ActionContext.getContext().getSession().get(Constant.LOGIN_USER_KEY);

             

        //如果是用戶(hù),設(shè)定組織編號(hào)。如果是領(lǐng)導(dǎo),則通過(guò)參數(shù)給組織編號(hào)賦值。

        if( (null == teamId) || ("".equals(teamId))){

            this.setTeamId(user.getOrgan().getId());

      

 

}       

       

        int perPage = 10;

        String totalHql =

            "select count(*) from Log as log where log.user.organ.id='"+teamId+"'";

              PageHelper pageHelper = new PageHelper();

              if(perPageCount != -1){

                  perPage = pageHelper.doCustomPerPage(perPageCount);

              }

          

              Page pageObj = new Page();

              pageObj.setPerPage(perPage);

              pageObj.setPage(pageNo);

              pageObj.setHql(" from Log log order by log.id desc where log.user.organ.id='"+teamId+"'");

              pageObj.setTotal(this.logDao.getTotal(totalHql));

        if(user.isLeader()){   

               this.setTeams(logDao.listTeams());

               pageHelper.setHaveParam(true);

               pageObj.setUrl("listlogs"+Constant.FILE_SUFFIX+"?teamId="+teamId);

        }else{

            pageObj.setUrl("listlogs"+Constant.FILE_SUFFIX);

        }

              pageObj.setStartRs((pageNo-1) * perPage);

 

              page = pageHelper.getNextPageObject(pageObj);

             

        //列出組織

        this.setLogs(this.logDao.getList(page));

 

        //根據(jù)組織ID獲得名稱(chēng)

        this.setTeamName(logDao.getNameById(teamId));

 

        if (logger.isDebugEnabled()) { 

            logger.debug("載入session用戶(hù)成功!");

        }       

        return SUCCESS;

    }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多