问题描述
我想在 组件中显示验证消息和来自支持 bean 的消息.在我的 JSF 页面中,我定义了以下对话框:
I want to present validation messages and messages from backing bean in <p:dialog>
component. In my JSF page I have following dialog defined:
<p:dialog widgetVar="messageDialog" id="msgDialog" modal="true" appendToBody="true">
<h:form id="messageForm">
<p:messages id="messagesInDialog" />
<p:commandButton value="OK" onclick="messageDialog.hide()" />
</h:form>
</p:dialog>
我在后台 bean 中附加一些消息后执行以下代码:
I execute the following code after appending some message in backing bean:
RequestContext.getCurrentInstance().execute("messageDialog.show()");
它工作正常.
但是,我还想在此对话框中显示 bean 验证消息.消息在验证后附加到 <p:message>
组件,但我不知道如何在验证失败后显示此对话框.
However, I also want to display bean validation messages in this dialog. Messages are appended to <p:message>
component afer validation, but I have no idea how to display this dialog after validation failure.
我怎样才能做到这一点?
How can I achieve this?
推荐答案
您可以使用 的
visible
属性来指定对话框是否应该默认显示与否.您可以通过 FacesContext#isValidationFailed()
如果验证失败.
You can use the visible
attribute of <p:dialog>
to specify whether the dialog should show up by default or not. You can check by FacesContext#isValidationFailed()
if there's a validation failure or not.
所以,简而言之:
<p:dialog id="msgDialog" widgetVar="messageDialog" modal="true" appendToBody="true"
visible="#{facesContext.validationFailed}">
<p:messages id="messagesInDialog" />
<p:button value="OK" onclick="messageDialog.hide()" />
</p:dialog>
(注意我用p:button
简化了不必要的h:form
和p:commandButton
)
(note that I simplified the unnecessary h:form
and p:commandButton
by a p:button
)
然后由以下人员更新:
<p:commandButton value="submit" update=":msgDialog" />
或者只是将它放在 <p:outputPanel autoUpdate="true">
中,以便它在每个 ajax 请求上自动更新自己,而无需在每个 update
属性:
Or by just placing it inside a <p:outputPanel autoUpdate="true">
so that it auto-updates itself on every ajax request without the need to specify it in every update
attribute:
<p:outputPanel autoUpdate="true">
<p:dialog id="msgDialog" widgetVar="messageDialog" modal="true" appendToBody="true"
visible="#{facesContext.validationFailed}">
<p:messages id="messagesInDialog" />
<p:button value="OK" onclick="messageDialog.hide()" />
</p:dialog>
</p:outputPanel>
另见:
- < 的呈现属性和可见属性之间的差异p:dialog>
与具体问题无关,覆盖非验证消息,例如您在操作方法中添加的那些全局消息,而不是检查是否FacesContext#getMessageList()
不为空.
Unrelated to the concrete problem, to cover non-validation messages, such as those global messages which you add in the action method, rather check instead if FacesContext#getMessageList()
is not empty.
<p:dialog ... visible="#{not empty facesContext.messageList}">
当有任何消息时,这将显示对话框.这样 RequestContext#execute()
调用是不必要的.
This will then show the dialog when there is any message. This way that RequestContext#execute()
call is unnecessary.
这篇关于在验证失败时自动在 p:dialog 中显示验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!