2

我在 JBoss AS 7.1 上使用 JSF 和 PrimeFaces 3.2。

问题:我无法将验证结果(不是常规的 JSF 验证!)从后端移交给前端。

我有一个绑定到实体的表单,当单击保存按钮时,我想对实体进行一些验证:

<p:growl id=saveSuccessDialog" />

<p:dialog widgetVar="verificationDialog" modal="true"
    header="#{msg['dialog.title.verificationError']}" resizable="false">
    <h:form id="verificationErrorForm">
        <h:outputText value="#{verificationResult.problemDescription}" />
    </h:form>
</p:dialog>

<h:form>
...some fields...
<p:commandButton
    actionListener="#{adminBean.saveObject}"
    value="Save" icon="ui-icon-check"
    onerror="verificationDialog.show();"
    update=":verificationErrorForm :saveSuccessDialog" />
</h:form>

现在,bean 的动作是这样的:

public void saveObject(ActionEvent event) {
    this.verifyObject();
    if (verificationResult.isValidConfiguration()) {
        FacesContext.getCurrentInstance().addMessage("Object successfully saved", new FacesMessage("Saved")); // this is correctly shown in the growl
        objectDao.save(this.sessionBean.getSelectedObject());
    } else {
        FacesContext.getCurrentInstance().getExternalContext().setResponseStatus(500);
    }
}

和验证结果:

@Named
@ConversationScoped
public class VerificationResult implements Serializable {
    private String problemDescription; // with get/set
    ... some more information about the problem here ...
}

我希望,意图很明确:当用户单击“保存”时,验证将运行(有效),如果验证失败,应弹出一个对话框,其中包含其他信息。如果验证成功,则会出现“保存成功”的咆哮声。这也有效。

但是如果显示验证错误对话框,verificationResult.problemDescription尽管它已设置,但它是空的。

所以,有两个问题:a)我该如何解决?b) 是否有更好的解决方案来处理此类要求?

我希望,这个问题是可以理解的。提前致谢!

4

1 回答 1

2

经过几个小时的搜索,我终于在 PrimeFaces 论坛上找到了解决方案:http: //forum.primefaces.org/viewtopic.php? f=3&t=16769

基本思想是将评估(是否显示对话框)安排到visible对话框本身的 - 属性中,然后让对话框update在 commandButton/Link 的 - 属性中更新。

感谢 BalusC 在另一个论坛上提出了这个问题 :-)

于 2012-05-18T10:55:09.540 回答