2014-12-29 66 views
0

我被要求編寫一個處理結果分頁的JSF組件。組件接受一個實現接口的bean。在運行時,getter方法被調用,但從來不是setters。下面是代碼JSF只調用getter,但從不設置複合組件

<cc:interface> 
    <cc:attribute name="pagableResultsBean" /> 
</cc:interface> 

<!-- Question: We can't have a form inside of a form. Do we ecapsulate form operations here or 
    should the client handle them? --> 
<cc:implementation> 
    <c:set var="pagableResultsBean" value="#{cc.attrs.pagableResultsBean}" /> 
    <div 
     style="background-color: #F0F3FA; border: 1px solid #ABABAB; height: 25px; margin: 0px; padding: 2px 8px; position: relative; vertical-align: middle;"> 
     <h:panelGroup> 
      <div style="float: left; width: auto;"> 
       Showing Results <h:outputLabel value="#{pagableResultsBean.showingRecordMin}" /> - <h:outputLabel value="#{pagableResultsBean.showingRecordMax}" /> of 
       #{pagableResultsBean.totalRecordsNum} Results per Page: 
       <h:selectOneMenu value="#{pagableResultsBean.resultsPerPage}" onchange="$j(document).find('.pageBarSubmitBtn').click();"> 
        <f:selectItems value="#{pagableResultsBean.resultsPerPageItems}" /> 
       </h:selectOneMenu> 
      </div> 
      <div style="float: right; width: auto;"> 
       Showing Page #{pagableResultsBean.currentPageNum} of #{pagableResultsBean.totalPagesNum} Jump 
       to Page 
       <h:selectOneMenu id="_pagesMenu" value="#{pagableResultsBean.currentPageNum}" onchange="$j(document).find('.pageBarSubmitBtn').click();"> 
        <f:selectItems value="#{pagableResultsBean.resultsPerPageItems}" /> 
       </h:selectOneMenu> 

       <h:commandLink style="position: relative; top: 3px;" action="#{pagableResultsBean.goToFirstPage()}"> 
        <h:graphicImage value="/images/arrow-first.gif" /> 
       </h:commandLink> 
       <h:commandLink style="position: relative; top: 3px;" action="#{pagableResultsBean.goToPreviousPage()}"> 
        <h:graphicImage value="/images/arrow-previous.gif" /> 
       </h:commandLink> 
       <h:commandLink style="position: relative; top: 3px;" action="#{pagableResultsBean.goToNextPage()}"> 
        <h:graphicImage value="/images/arrow-next.gif" /> 
       </h:commandLink> 
       <h:commandLink style="position: relative; top: 3px;" action="#{pagableResultsBean.goToLastPage()}"> 
        <h:graphicImage value="/images/arrow-last.gif" /> 
       </h:commandLink> 

      </div> 
      <h:commandButton style="visibility: hidden;" styleClass="jsfHidden pageBarSubmitBtn"/> 
     </h:panelGroup> 
    </div> 
</cc:implementation> 

這是支持bean實現的接口:

public interface PagableResults { 
    public int getShowingRecordMin(); 
    public int getShowingRecordMax(); 

    public ResultsPerPage getResultsPerPage(); 
    public void setResultsPerPage(ResultsPerPage rpp); 

    public int getCurrentPageNum(); 
    public void setCurrentPageNum(int pageNum); 

    public int getTotalPagesNum(); 
    public int getTotalRecordsNum(); 

    public List<SelectItem> getResultsPerPageItems(); 
    public List<SelectItem> getResultsPagesItems(); 

    public void goToFirstPage(); 
    public void goToLastPage(); 
    public void goToPreviousPage(); 
    public void goToNextPage(); 
} 

這裏是第一個使用該控件的代碼片段

<h:form> 
    <ndar:pagingBar pagableResultsBean="#{pageBean}" /> 
</h:form> 

任何人都可以看到任何理由爲什麼只有getter被調用而不是setter?

回答

0

由於您似乎使用JSP JSTL Core標記庫中的c:set,這可能無法很好地適用於JSF,尤其是JSF Composite Component系統。

您是否試圖用cc.attrs.pagableResultsBean替換pagableResultsBean的所有實例,因爲這樣可以消除一個可能的錯誤原因?

另外,將一個調試器放在你認爲沒有被調用的set-method上,並驗證它是否真的沒有調用,或者只是html結果。

更新2014-12-31

還要確保有一個h:messages標籤到JSF,以確保JSF告訴你一個可能的轉換錯誤的。

+0

我已經按照你的建議完成了,但這沒有什麼區別。此外,我也對二傳手方法提出了一個斷點,並證實他們沒有被調用。 – user2980252