【CSII-PE】PE分頁標籤實現分析
阿新 • • 發佈:2018-12-31
PE分頁標籤實現分析
PE中通過自定義標籤提供了分頁功能(前臺部分)。前臺要實現分頁,主要就是需要向後臺傳遞將要展示的頁數編號。
<pe:combinedPage total="${recordNumber}" pageSize="${pageSize}" formName="form2" ajax="true" script="true" action="BaoAcctErrorList.do" fieldList="BeginDate,EndDate"/> <table width="100%" border="0" cellpadding="0" cellspacing="0" > <tr > <td class="pagesLinks" width="100%" align="right"> 當前第<b><pe:PageNo/></b>頁 共<b><pe:PageNumber/></b>頁 <b><pe:RecordNumber/></b>條記錄 <pe:GotoPage type='1' insertContent="class='xxx'" insertContent2="class='yyy' "/> <pe:TopPage type='2' value="Page.TopPage" /> <pe:PrevPage type='2' value="Page.PrevPage"/> <pe:NextPage type='2' value="Page.NextPage" /> <pe:BottomPage type='2' value="Page.BottomPage" /> </td> </tr> </table>
這事pe中常用的前臺頁面程式碼結構,接下來就分析一下這些標籤的程式碼,看看是如何實現分頁的。
首先是combinedPage這個標籤:
PageBean pageBean = new PageBean(); pageBean.xhtml = xhtml; pageBean.jsDisabled = jsDisabled; pageBean.scriptSuffix = this.scriptSuffix; pageBean.action = this.action; pageBean.hiddenFieldName = this.hiddenFieldName; pageBean.hiddenTotalFieldName = this.hiddenTotalFieldName; this.pageContext.setAttribute(this.var, pageBean, 1); pageBean.recordNumber = 0; pageBean.pageSize = this.defaultPageSize; //記錄總數(標籤屬性傳遞) Object tmpObject = this.total; if (tmpObject != null){ if ((tmpObject instanceof Number)) { pageBean.recordNumber = ((Number)tmpObject).intValue(); } else{ String tmpStr = tmpObject.toString(); if (tmpStr.equals("")){ System.err.println("CombinedPageTag: total is invalid!, pls delete this tag or use pagable action"); return 0; } pageBean.recordNumber = Integer.parseInt(tmpStr); } }else{ String tmpStr = this.pageContext.getRequest().getParameter(this.hiddenTotalFieldName); if (tmpStr != null){ pageBean.recordNumber = Integer.parseInt(tmpStr); }else{ System.err.println("CombinedPageTag: total is invalid!!, pls delete this tag or use pagable action"); return 0; } } //pageSize標籤屬性 Object pageSizeObject = this.pageSize; if (pageSizeObject != null){ if ((pageSizeObject instanceof Number)){ pageBean.pageSize = ((Number)pageSizeObject).intValue(); }else { String tmpStr = pageSizeObject.toString(); pageBean.pageSize = Integer.parseInt(tmpStr); } } //總頁數 pageBean.pageNumber = ((pageBean.recordNumber + pageBean.pageSize - 1) / pageBean.pageSize); int pageNo = 0; //當前頁數 Object currentIndexInRequest = this.pageContext.getAttribute(this.hiddenFieldName, 2); if (currentIndexInRequest != null){ if ((currentIndexInRequest instanceof Number)){ pageNo = ((Number)currentIndexInRequest).intValue(); }else{ pageNo = Integer.parseInt(currentIndexInRequest.toString()); } }else{ String currentIndex = this.pageContext.getRequest().getParameter(this.hiddenFieldName); if (currentIndex != null){ pageNo = Integer.parseInt(currentIndex); } } pageBean.pageNo = (pageNo / pageBean.pageSize + 1); pageBean.prevPageIndex = pageNo; pageBean.nextPageIndex = pageNo; pageBean.lastPageIndex = ((pageBean.pageNumber - 1) * pageBean.pageSize); if (pageNo >= pageBean.pageSize){ pageBean.prevPageEnable = true; pageBean.prevPageIndex = (pageNo - pageBean.pageSize); } if (pageNo + pageBean.pageSize < pageBean.recordNumber) { pageBean.nextPageEnable = true; pageBean.nextPageIndex = (pageNo + pageBean.pageSize); }
這個標籤的程式碼量較大,主要作用就是根據頁面上設定的屬性(total,pageSize)來計算總共的頁數,並且輸出需要的js程式碼,供下面的標籤呼叫,同時通過一個PageBean物件來儲存一些分頁相關的資料。
下面看下當前頁號標籤PageNo的程式碼:
public int doStartTag() throws JspException { try{ PageBean pageBean = (PageBean)this.pageContext.getAttribute(this.var, 1); if (pageBean == null){ this.pageContext.getOut().print("pls define combinedPage Tag before:" + getClass().getName()); }else { this.pageContext.getOut().print(pageBean.getPageNo()); } }catch (Exception ex){ throw new JspException("pe.http.tag_fail_to_write", ex); } return 0; }
下一頁標籤PrePage的程式碼:
public int doStartTag()throws JspException{
PageBean pageBean = (PageBean)this.pageContext.getAttribute(this.var, 1);
if (pageBean == null){
try {
this.pageContext.getOut().print("pls define combinedPage Tag before:" + getClass().getName());
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
if ((!pageBean.isPrevPageEnable()) && (this.hidden)){
return 0;
}
if (pageBean.jsDisabled) {
return doStartTagWithoutJS(pageBean);
}
return doStartTagForDefault(pageBean);
}
private int doStartTagForDefault(PageBean pageBean) throws JspException {
try{
StringBuffer sb = new StringBuffer();
if (this.type == 1){
sb.append("<input ");
sb.append(this.insertContent);
sb.append("type='button' ");
sb.append("id='PrevPage' ");
sb.append("name='PrevPage' ");
sb.append("value='");
sb.append(format(this.value));
sb.append("' ");
}else if (this.type == 2){
sb.append("<a href=\"####\" ");
sb.append(this.insertContent);
}
sb.append(" ");
if (pageBean.isPrevPageEnable()){
sb.append("onClick=\"pe_changePage").append(pageBean.scriptSuffix).append("(this,");
sb.append(pageBean.getPrevPageIndex());
sb.append(")\"");
}else if (pageBean.xhtml) {
sb.append("disabled=\"disabled\"");
} else {
sb.append("disabled");
}
sb.append(" ");
if (this.type == 1){
sb.append("/>");
} else if (this.type == 2){
sb.append(">");
sb.append(format(this.value));
sb.append("</a>");
}
this.pageContext.getOut().print(sb.toString());
} catch (Exception ex){
throw new JspException("pe.http.tag_fail_to_write", ex);
}
return 0;
}
其他標籤實現方式類似,這裡就不再分析了。
by [email protected]王大仙