2012-11-05 20 views
0

我點擊了一個p:commandButton,我需要在列表中添加一些值。在我的託管bean中,我正在驗證必須添加的值,如果驗證爲false,則必須顯示確認彈出窗口。這是我的代碼 -根據條件在按鈕單擊上顯示確認彈出框

<p:commandButton id="add" value="Add" type="submit"          action="#{bean.doAdd}" ajax="false" 
update=":List"/> 

而且在豆, 「添加」 按鈕,點擊,

public String doAdd() throws Exception { 
     if(response != null) { 
      if(keyList.contains(response)) { 
       if(!responseList.contains(response)) { 
        responseList.add(response); 
       } 
      } else { 
       //Have to display confirmation popup. 
      } 
      response = ""; 
     } 

     return response; 
    } 

我使用JSF 2.0和3.0 primefaces。有人可以告訴我如何顯示來自bean的彈出窗口嗎?

回答

2

您可以使用RequestContext到託管bean中運行的js代碼

確保其Ajax調用 - 有沒有ajax="false"

這樣

RequestContext context = RequestContext.getCurrentInstance(); 
context.execute("YourDialogwidgetVar.show()"); 

我假設你有定義了一些對話...

<p:confirmDialog id="confirmDialog" message="Hello" 
       header="Header" widgetVar="YourDialogwidgetVar"> 
</p:confirmDialog> 
+0

你有項目中包括黃金素質嗎?添加'import org.primefaces.context.RequestContext;'到你的bean ... – Daniel

+0

我確實包含了primefaces,之前使用了錯誤的導入。更正了它並嘗試了你的建議,但它拋出了空指針異常。 – Raaz

+0

是你的bean一個託管bean嗎?像這樣:'@ManagedBean @ SessionScoped' – Daniel

0

此代碼可能對你有幫助。

private boolean valid = true; 

public void doAdd() { 
    valid = false; 
} 


<p:dialog id="basicDialog" header="Basic Dialog" visible="#{!testBean.valid}"> 
    <h:outputText value="Message!" /> 
</p:dialog> 

<h:commandButton id="modalDialogButton" value="Modal" action="#{testBean.doAdd}"/> 
相關問題