我在 JBoss AS 7.1 上使用 JSF 和 PrimeFaces 3.2。
问题:我无法将验证结果(不是常规的 JSF 验证!)从我的后端移交给我的前端。
我有一个绑定(bind)到实体的表单,当单击保存按钮时,我想对实体进行一些验证:
<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 的 Action 是这样的:
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) 有没有更好的解决方案来处理此类需求?
我希望这个问题是可以理解的。提前致谢!
最佳答案
经过几个小时的搜索,我终于在 PrimeFaces 论坛上找到了解决方案: http://forum.primefaces.org/viewtopic.php?f=3&t=16769
基本思想是安排对对话框本身的 visible
属性进行评估(是否应显示对话框),然后让对话框在 update 中更新
-命令按钮/链接的属性。
感谢 BalusC 在其他论坛上提出了这个问题:-)
关于jsf - 出现错误时显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10649472/