2013-01-07 21 views
0

我想我碰到了一個@ViewScopeda4j:commandButton的錯誤。驗證ViewParam和a4j:commandButton

我有一個非常複雜的表單,其中所有操作都使用a4j,除了那些需要上傳數據的表單。 取決於命令的順序,viewParam的驗證會中斷。

這裏是工作代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:a4j="http://richfaces.org/a4j" > 

    <f:metadata> 
    <f:viewParam id="viewParam" name="viewParam" value="#{bean.viewParam}" required="true" /> 
    </f:metadata> 
    <h:head> 
    <title>Test View Param</title> 
    </h:head> 
    <h:body> 
    <h:message for="viewParam" /> 
    <hr/> 
    <h:form> 
     #{bean.viewParam}<br/> 
     <h:commandButton value="cmdButton" /> 
     <a4j:commandButton value="a4jBtn" execute="@this" render="@form" /> 
    </h:form> 
    </h:body> 
</html> 

只需點擊a4jBtn,然後在cmdButton看問題。 你會看到參數仍然存在!但驗證失敗。

<t:saveState>沒有幫助, <rich:message>也並不好,但

<h:commandButton value="ajaxBtn" ><f:ajax execute="@this" render="@form" /></h:commandButton> 

<a4j:commandButton value="a4jBtn" execute="@this" render="@form" /> 

,而不是不正常工作!

在Tomcat 6.0.18和jboss-el 2.0.0.GA上使用myFaces 2.0.15和richFaces 4.2.3.Final。

我可以通過使用f:ajax而不是a4j:commandButton來解決我的問題,但也許您有更好的想法,或者您可以向我解釋發生了什麼問題?

+0

通過使用omnifaces和修復。非常感謝@BalusC。 – mmoossen

+0

不客氣。我已經看到這個問題,並且已經知道''會做。我不確定爲什麼我沒有麻煩回答它。也許是因爲我不想把「另一個圖書館」推到喉嚨裏:)如果你在將來的問題中提到你也在使用OmniFaces,可能會有所幫助。 – BalusC

+0

@BalusC:好的,對。我將來會提及OmniFaces。但現在請寫一個答案! – mmoossen

回答

1

您基本上需要在同步回發中保留視圖參數。當你使用OmniFaces時,你可以使用<o:form>

<o:form includeViewParams="true"> 

或者,因爲您已經在使用視圖範圍的bean,因此僅在非回發時觸發驗證。

<f:viewParam ... required="#{not facesContext.postback}" /> 

或者,因爲你正在使用OmniFaces,你可以使用<o:viewParam>代替它會跳過回發確認/轉換/更新。

<o:viewParam ... />