【问题标题】:How to show confirm (richfaces) popup when use action Method in JSF Managed Bean?在 JSF Managed Bean 中使用 action Method 时如何显示确认(richfaces)弹出窗口?
【发布时间】:2012-08-20 21:01:37
【问题描述】:

也许我们熟悉 Javascript 中的确认弹出窗口。但是,在这个例子中,我想在 RichFaces 中基于 popupPanel 创建一个自定义确认弹出窗口,它有两个要求:

我可以描述一下确认框的要求如下:

  1. 在我们的应用程序有效数据(由ManagedBean中的一种action方法处理)之后,出现自定义确认框,要求用户确认向数据库插入数据。

    • 不像我们使用window.confirm时,点击页面中的按钮后出现确认框。

  2. 用户确认确认此窗口,向数据库插入数据要实现的动作方法

我现在想到的一种解决方案是使用两种操作方法来处理一个弹出窗口。当我解决我的问题时,我会在这个问题中通知你。谢谢。

【问题讨论】:

    标签: jsf-2 richfaces


    【解决方案1】:

    是的,一种方法是使用两种操作方法:一种用于验证,另一种用于实际操作。这实际上是我自己为某些项目所做的方式。

    实现此目的的一个好方法是创建一个包装此功能的复合组件,以便您可以在其他地方重用它。

    WebContent/resources/my/confirmButton.xhtml中创建一个复合组件,代码如下:

    resources/my/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

    【讨论】:

      猜你喜欢
      • 2011-02-16
      • 2012-10-13
      • 2014-02-04
      • 2012-02-20
      • 2011-10-31
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多