java 从 FacesContext 获取当前的全局消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7507729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the current global messages from FacesContext
提问by cesaregb
I have a problem. I need to know whether my page has global errors or not. This is because I have 2 different h:messages
(error containers)
我有个问题。我需要知道我的页面是否有全局错误。这是因为我有 2 个不同的h:messages
(错误容器)
<h:messages id="errorMsgsContainer"
layout="table"
rendered="true"
styleClass="message-container error-message"
infoClass="info"
errorClass=" error" warnClass="warn-message warn" globalOnly="true"/>
<h:messages id="errorMsgsContainerValidation"
layout="table"
styleClass="message-container error-message-validation"
infoClass="info"
errorClass="error" globalOnly="false"/>
One will show the business related messages and the other will just show the validation messages. There are two messages
because of business requirements.
一个将显示与业务相关的消息,另一个将仅显示验证消息。有两个messages
是因为业务需求。
When validation error messages are produced, the facelet works fine, because one of the messages
tag has the globalOnly="true"
attribute-value pair.
生成验证错误消息时,facelet 工作正常,因为其中一个messages
标记具有globalOnly="true"
属性-值对。
The problem comes when I've a global-only error. It will appear in both boxes.
当我有一个仅限全局的错误时,问题就来了。它将出现在两个框中。
I want to know if any of there errors are global, so I don't show the validation container till the global errors are fixed by the user on my form.
我想知道是否有任何错误是全局的,因此在用户在我的表单上修复全局错误之前,我不会显示验证容器。
I've tried to get it through the FacesContext
with
我试图通过FacesContext
与
FacesContext.getCurrentInstance().getMessageList().get(i).getSeverity()
and some other commands but it does not seem to work.
和其他一些命令,但它似乎不起作用。
Please help me to solve this problem. How can I get the current global messages list, so I can know if there is any global error?
请帮我解决这个问题。如何获取当前的全局消息列表,以便知道是否存在任何全局错误?
采纳答案by Vineet Reynolds
When validation error messages are produced, the facelet works fine, because one of the messages tag has the globalOnly="true" attribute-value pair.
生成验证错误消息时,facelet 工作正常,因为其中一个消息标记具有 globalOnly="true" 属性-值对。
This is incorrect. You are seeing messages for validation errors, in the other h:messages
tag with the globalOnly="false"
attribute-value pair. Validation messages always have a client Id, which happens to be the Id of the form element that failed validation, and hence will be displayed in a messages
tag that allows non-global messages to be displayed, or has the value of the for
attribute set to the applicable Id.
这是不正确的。您会在h:messages
带有globalOnly="false"
属性-值对的另一个标记中看到验证错误的消息。验证消息总是有一个客户端 ID,它恰好是验证失败的表单元素的 ID,因此将显示在messages
允许显示非全局消息的标记中,或者将for
属性值设置为适用的 ID。
The problem comes when I've a global-only error. It will appear in both boxes.
当我有一个仅限全局的错误时,问题就来了。它将出现在两个框中。
This is expected behavior. I believe you've confused the meaning of the globalOnly
attribute. When the value of the globalOnly
attribute is true, only global messages (i.e. messages without a client Id) will be displayed; when the value is false, global messages will be displayed in addition to other messages that are already queued. This would explain why global messages are displayed twice - the first h:messages
tag would display the global message because it should display only global messages, and the second would display it because it can display it.
这是预期的行为。我相信你已经混淆了globalOnly
属性的含义。当globalOnly
属性值为true时,只会显示全局消息(即没有客户端Id的消息);当值为 false 时,除了已经排队的其他消息之外,还会显示全局消息。这将解释为什么全局消息会显示两次——第一个h:messages
标签会显示全局消息,因为它应该只显示全局消息,第二个标签会显示它,因为它可以显示它。
Please help me to solve this problem. How can I get the current global messages list, so I can know if there is any global error?
请帮我解决这个问题。如何获取当前的全局消息列表,以便知道是否存在任何全局错误?
If you want to continue having two h:messages
tags in your facelet, then you can use a "pseudo-global" Id when queuing your FacesMessage
s for display, instead of specifying an Id of null
; the value of the pseudo-global Id in the following example is inputForm
which is a valid client Id (of the form) that would not have any validation messages produced in this case:
如果你想h:messages
在你的 facelet 中继续有两个标签,那么你可以在排队FacesMessage
显示时使用“伪全局”Id ,而不是指定 Id null
; 以下示例中伪全局 Id 的值是inputForm
在这种情况下不会产生任何验证消息的有效客户端 Id(形式):
FacesContext.getCurrentInstance().addMessage("inputForm", new FacesMessage(FacesMessage.SEVERITY_INFO, "Added a global message", null));
You can then add a EL expression to render the messages
tag responsible for display of the input-validation messages:
然后,您可以添加一个 EL 表达式来呈现messages
负责显示输入验证消息的标签:
<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:fn="http://java.sun.com/jsp/jstl/functions">
...
<h:form id="inputForm">
...
</h:form>
<h:messages id="psuedoGlobalMessages" for="inputForm" globalOnly="true" infoStyle="color:green" errorStyle="color:red" warnClass="color:orange" />
<h:messages id="clientMessages" rendered="#{fn:length(facesContext.getMessageList('inputForm')) == 0}" globalOnly="false" infoStyle="color:green" errorStyle="color:red" warnClass="color:orange" />
...
Note, the use of the globalOnly
attribute in only one messages
tag. The same messages
tag is also not displayed if a pseudo-global message is queued up for display via the EL expression specified in the rendered
attribute. You can also use the client Id of a hidden form element created specifically to direct all pseudo-global messages, instead of the form's client Id.
注意,该globalOnly
属性仅在一个messages
标签中使用。messages
如果伪全局消息通过rendered
属性中指定的 EL 表达式排队显示,则同样的标记也不会显示。您还可以使用专门创建的隐藏表单元素的客户端 ID 来引导所有伪全局消息,而不是表单的客户端 ID。
回答by Aleksei Loginov
Try this:
试试这个:
rendered="#{not empty facesContext.getMessageList('inputForm')}
instead of:
代替:
rendered="#{fn:length(facesContext.getMessageList('inputForm')) == 0}"
in the Vineet Reynolds's answer.
在 Vineet Reynolds 的回答中。