2013-07-14 30 views
6

我想使<p:calendar>只讀,以便用戶只能從日曆中選擇一個日期,因爲this問題(但這不是解決方法)。使p:日曆只讀

對於這個是這樣,我做readonly="#{facesContext.renderResponse}"this答案就像提到,

<p:calendar id="calendarId" 
     value="#{bean.property}" 
     converter="#{jodaTimeConverter}" 
     pattern="dd-MMM-yyyy hh:mm:ss a" 
     showOn="button" 
     readonly="#{facesContext.renderResponse}" 
     effect="slideDown" 
     required="true" 
     showButtonPanel="true" 
     navigator="true"/> 

這工作,但在頁面加載(輸入在地址欄中的URL,然後按回車鍵),facesContext.renderResponse返回false並且日曆不再只讀。當我通過按<p:commandButton>提交表單時,它的計算結果爲true

那麼,如何使日曆只讀,當頁面加載?

P.S:我正在使用PrimeFaces 3.5和Mojarra 2.1.9。

回答

11

自JSF 2.0以來的行爲確實發生了變化。 FacesContext#getRenderResponse()只返回true如果FacesContext#renderResponse()明確被調用。以前這發生在每個GET請求的恢復視圖階段。但是,自引入<f:viewParam>以來,當至少存在一個視圖參數時,JSF不會再這樣做,它將繼續執行每個階段而不跳過任何階段,以正確處理視圖參數。

顯然你的頁面中有一個<f:viewParam>。這是完全正常的,但作爲一個測試,嘗試刪除它,你會發現它也返回一個普通的GET請求true

您已經基本上採用兩種方法來繞過它:

  1. 檢查FacesContext#isPostback()爲好。它始終在GET請求上返回false

    readonly="#{not facesContext.postback or facesContext.renderResponse}" 
    
  2. 改爲檢查FacesContext#getCurrentPhaseId()。你只會得到更醜陋的代碼(幻數)。

    readonly="#{facesContext.currentPhaseId.ordinal eq 6}" 
    

    如果您使用OmniFaces,你可以使其不太難看。

    <o:importConstants type="javax.faces.event.PhaseId" /> 
    ... 
    readonly="#{facesContext.currentPhaseId eq PhaseId.RENDER_RESPONSE}" 
    
+0

這是非常好的解決方案:) –