gpt4 book ai didi

java - jsf1.2丰富:messages duplicating validation of rich:message in the same form

转载 作者:行者123 更新时间:2023-12-01 15:34:07 25 4
gpt4 key购买 nike

我正在使用 JSF/Spring webflow&mvc 应用程序准备一个注册页面。我添加了两个侧面验证,一个在客户端,每个输入字段都有自己的 rich:message,另一个在服务器端,注册 bean 响应为 facescontext.addMessage()。当我尝试测试验证时,我的问题是,rich:message 组件显示错误正确,但 rich:messages 也显示相同的错误。我想仅针对特定错误使用消息,例如数据库中已存在邮件之类的消息。

我的丰富:消息标签:

<rich:messages id="msg" layout="table" limitRender="true"
style="font-weight:bold;color:#BDBDBD">
<f:facet name="errorMarker">
<h:graphicImage url="/images/messages_error.png" />
</f:facet>
</rich:messages>

<h:form ...>
.....SOME LOGIC.....

<h:inputText id="i_ln" required="true" value="#{regBean.lastName}">
<f:validateLength minimum="2" maximum="35" />
<rich:ajaxValidator event="onblur" />
</h:inputText>
<rich:message for="i_ln">
<f:facet name="errorMarker">
<h:graphicImage url="/images/messages_error.png" />
</f:facet>
</rich:message>
.....
</h:form>

我已经检查了人们建议使用 globalOnly="true"的类似问题,当我使用此属性时,重复验证会停止,但我无法添加严重性错误。我的输出如下:

Feb 07, 2012 10:08:04 PM com.sun.faces.lifecycle.RenderResponsePhase execute
INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=i_comment[severity=(ERROR 2), summary=(Eingabe wird benötigt.), detail=(Eingabe wird benötigt.)]
sourceId=i_title[severity=(ERROR 2), summary=(Eingabe wird benötigt.), detail=(Eingabe wird benötigt.)]
sourceId=i_salut[severity=(ERROR 2), summary=(Eingabe wird benötigt.), detail=(Eingabe wird benötigt.)]
sourceId=i_mobile[severity=(ERROR 2), summary=(Eingabe wird benötigt.), detail=(Eingabe wird benötigt.)]
sourceId=i_fax[severity=(ERROR 2), summary=(Eingabe wird benötigt.), detail=(Eingabe wird benötigt.)]

另一个问题是我应该保留服务器端验证吗?

最佳答案

好的,这里有一个解决方法: 首先,它是一个已知问题,请参阅jboss community :

Hi guys,

We investigated the issue further by debugging the RichFaces JavaScript code.

We noticed that both the <rich:messages globalOnly="true" /> component and the <rich:message for="inputField" /> component bind to an "onMessage" event.

When the event is triggered on tab, the "onMessage" function in "message.js.faces" is triggered for both components.

As you can see in the attached screenshots the function is called twice: once for both components. Other <rich:message /> components which have a "for" attribute related to other fields are not triggered, just as expected.
In the JavaScript code, we can see that a check is performed on the availability of the "forComponentId" attribute. If this attribute is available, a validation message is rendered for the specified component id. If this is not available, a global message is rendered.
But when a global message is rendered, we cannot see any check on the globalOnly attribute. So any message is rendered, regardless of this attribute. The attribute is nowhere to be found in the JavaScript code.

We were wondering if this is the bug or if there is an other piece of code that is responsible for checking whether only global error messages may be displayed?

所以我玩弄了代码并注意到 rich:messages 只是在以下情况下选择消息:

  1. 如果 globalOnly="true"clientId 为 null 或 clientId 是消息组件 ID。
  2. 如果for="xxx"属性已分配且消息具有 client&sourceID="xxx"
  3. 如果没有for="xxx"并且没有分配 globalOnly="true" 属性,则任何消息也包含空客户端 ID。

我能够为此应用两种解决方案:

针对只有一个 rich:messages 的页面的第一个解决方案;

我的 rich:messages 标记只有 globalOnly="true" 并且没有 for 属性;

   <rich:messages id="msg" layout="list"
style="font-weight:bold;color:#BDBDBD"
globalOnly="true">
<f:facet name="errorMarker">
<h:graphicImage url="/images/messages_error.png" />
</f:facet>
</rich:messages>

我的 bean 发送消息 id=null;

private void addSeverityError(String message)
{
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage fMessage = new FacesMessage();
fMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
fMessage.setSummary(message);
fMessage.setDetail("Validation error");
context.addMessage(null, fMessage);
}

针对具有多个 rich:messages 的页面的第二种解决方案;

我添加了一个隐藏的 rich:message 来用作桥梁,然后我的消息可以具有隐藏的组件 id 并且不会为空,在我使用此 id 通过我的 rich:messages 拾取它之后。

<rich:message id="nonExistClient" rendered="false"/>
<a4j:form id="messagesForm">
<rich:messages id="msg" layout="list"
style="font-weight:bold;color:#BDBDBD"
for="nonExistClient">
<f:facet name="errorMarker">
<h:graphicImage url="/images/messages_error.png" />
</f:facet>
</rich:messages>
</a4j:form>

我通过 FacesContext 添加了如下消息:

private void addSeverityError(String message)
{
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage fMessage = new FacesMessage();
fMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
fMessage.setSummary(message);
fMessage.setDetail("Validation error");
context.addMessage("nonExistClient", fMessage);
}

关于java - jsf1.2丰富:messages duplicating validation of rich:message in the same form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9183947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com