【问题标题】:How to keep a Primefaces dialog open when an error happens in an actionListener?当 actionListener 发生错误时,如何保持 Primefaces 对话框打开?
【发布时间】:2017-06-30 15:29:35
【问题描述】:

我有一个带有文本输入的p:dialog。保存时,输入的值将传递给外部服务,该服务可以接受或拒绝它。文本输入有一个验证器,但该验证器只能检查这么多。特别是,它不知道外部服务的状态。它无法调用该服务,因为在检查时间和保存时间之间输入可能变得无效。

到目前为止,我在页面和弹出窗口中有一个<h:messages>。弹出窗口有自己的形式。当弹出窗口关闭时,外部服务的验证错误出现在页面的<h:messages> 中,因为作为一种解决方法,我将页面的消息添加到命令按钮的update 属性中。弹出窗口的p:commandButton 使用ajax='true' 并检查验证错误(oncomplete="if (arg && !arg.validationFailed) PF('popup').hide()")作为explained in another post。看起来验证已经完成并且没有错误,所以弹出窗口关闭并运行按钮的actionListener 将输入推送到外部服务,然后从服务返回错误。

我知道在 setter 或 listener 中进行验证是一种反模式,但我在这里看不到解决方法。无论如何,它并没有太多的验证,它更多的是“接受这个”并准备接收它的错误。

我尝试从侦听器再次打开弹出窗口,但没有打开对话框。

<h:form id="Form">
    <h:messages id="pageErrors"/> <!-- external service error shows up here -->
<h:form>

<h:form>
    <p:dialog widgetVar="popup">
        <h:messages id="popErrors"/> <!-- I'd like to show the external service error here -->
        <p:inputTextarea id="it" required="true"/>
        <!-- required works as expected: error in the popup when nothing is entered, popup remains open -->
        <p:commandButton value="Save it" ajax="true" update="popErrors Form:pageErrors"
            oncomplete="if (args &amp;&amp; !args.validationFailed) PF('popup').hide();"
            actionListener="#{bean.saveIt}"/>
    </p:dialog>
</h:form>

还有actionListener:

public void saveIt(@SuppressWarnings("unused") ActionEvent e) {
    String error = extService.saveIt(it);
    if (error != null) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(error)); // gets the error in the page's messages
        facesUtil.showDialog("popup");
    }
}

actionListener 检测到来自外部服务的错误时,如何保持此对话框打开?

【问题讨论】:

  • 试过onsuccess而不是oncomplete?和/或从 actionListener 中关闭它?
  • 看看这个答案,它可能会对你有所帮助,因为在你的情况下,validationFailed 不是答案,你需要自己的错误处理stackoverflow.com/a/19834249/833031

标签: validation jsf primefaces


【解决方案1】:

如果我理解这个问题,这对我有用:

update="popErrors :Form:pageErrors"

FacesContext.getCurrentInstance().validationFailed();

在您添加消息之后。

在响应中添加 FacesMessage 不等于验证失败。如果您验证自己,则必须手动告诉框架是否失败。

您也可以按照 Hatem 的建议添加自己的 callback parameter,但据我所知,重复使用 validationFailed 没有问题。

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多