2012-08-25 19 views
0

也許我們熟悉Javascript中的確認彈出窗口。然而,在這個例子中,我希望創建一個有2個要求,RichFaces的基礎上popupPanel一個自定義的確認彈出:如何在JSF託管Bean中使用Action時顯示確認(richfaces)彈出窗口?

我可以描述的確認框的要求如下:

  1. 之後,我們的應用程序有效數據(通過ManagedBean中的一個操作方法處理),然後出現自定義確認框,並要求用戶確認向數據庫插入數據。

    • 不像我們使用window.confirm時,點擊頁面中的按鈕後出現確認框。

  2. 用戶確認自己是否確認此窗口中,要實現將數據插入到數據庫

操作方法我覺得現在有一種解決辦法是使用兩個處理法處理一個彈出。當我解決我的問題時,我會在這個問題上通知你。謝謝。

回答

0

是的,一種方法是使用兩種操作方法:一種用於驗證,另一種用於實際操作。實際上,我一直在爲一些項目自己做這件事。

這樣做的一個好方法是創建一個包裝此功能的複合組件,以便您可以在其他位置重新使用它。

創建WebContent/resources/my/confirmButton.xhtml的複合成分,用下面的代碼:

資源/我的/ confirmButton.xhtml

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich" 
    xmlns:cc="http://java.sun.com/jsf/composite"> 
<cc:interface> 
    <cc:attribute name="message" default="Do you want to proceed?" /> 
    <cc:attribute name="messageHeader" default="Confirm" /> 
    <cc:attribute name="action" required="true" 
      method-signature="java.lang.String action()" /> 
    <cc:attribute name="validateMethod" required="true" 
      method-signature="java.lang.String action()" /> 
    <cc:attribute name="value" default="Submit" /> 
    <cc:attribute name="cancelBtn" default="No" /> 
    <cc:attribute name="confirmBtn" default="Yes" /> 
</cc:interface> 
<cc:implementation> 
    <rich:popupPanel id="popup" header="#{cc.attrs.messageHeader}"> 
     <p>#{cc.attrs.message}</p> 
     <input type="button" value="#{cc.attrs.cancelBtn}" 
      onclick="#{rich:component('popup')}.hide()" /> 
     <a4j:commandButton action="#{cc.attrs.action}" 
      value="#{cc.attrs.confirmBtn}" execute="@form" render="@form" 
      onclick="#{rich:component('popup')}.hide()" /> 
    </rich:popupPanel> 
    <a4j:commandButton action="#{cc.attrs.validateMethod}" 
     value="#{cc.attrs.value}" execute="@form" render="@form" 
     oncomplete="if(#{empty facesContext.maximumSeverity}){ 
      #{rich:component('popup')}.show() 
     }" /> 
</cc:implementation> 
</html> 

你可能想改變它,添加屬性和造型,你見必要。

做完,你可以聲明命名空間爲您的組件的JSF視圖,並使用和重用這樣的:

<!DOCTYPE html> 
<html ... xmlns:my="http://java.sun.com/jsf/composite/my"> 
    ... 
    <h:form> 
     <rich:messages /> 
     <h:panelGrid> 
      Please, type something: 
      <h:inputText value="#{someBean.someValue}" required="true" /> 

      <!-- simple example with the default labels: --> 
      <my:confirmButton action="#{someBean.action}" 
       validateMethod="#{someBean.preAction}" /> 

      <!-- second example calling other method and using custom labels: --> 
      <my:confirmButton action="#{someBean.action2}" 
       validateMethod="#{someBean.preAction}" value="action 2" 
       message="Do you REALLY want to proceed?" /> 
     </h:panelGrid> 
    </h:form> 
    ... 
</html> 

參見:

Composite Component tag info