jsf1.2 rich:messages 以相同的形式重复验证 rich:message

发布于 2025-01-03 04:40:43 字数 1885 浏览 3 评论 0原文

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

我的 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" 的类似问题,当我使用此属性时,重复验证会停止,但我无法添加严重错误。我的 oytput 如下:

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.)]

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

I am preparing a registration page using JSF/Spring webflow&mvc application. I have added two side validation one in client side every input fields has its own rich:message and other one at server side, registration bean response as facescontext.addMessage(). My problem when I try to test validation, rich:message components show errors correct but rich:messages show same errors as well. I want to use messages just for specific errors, something like mail already exist in database.

my rich:messages tag:

<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>

I have checked similar issues that people advice to use globalOnly="true" and when I use this attribute then duplicate validation stops but I am not able to add severity error. My oytput as below:

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.)]

Another question is should I keep server side validation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2025-01-10 04:40:43

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

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. if 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);
}

Ok here is a workaround:
First of all its a known issue refer to 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?

So I have played around with code and notice that rich:messages just picking messages when:

  1. if globalOnly="true" and clientId is null or clientId is messages component id.
  2. if for="xxx" attribute
    assigned and message has client&sourceID="xxx"
  3. if no for="xxx" and
    no globalOnly="true" attribute assigned then any message also messages with
    the null client id.

I was able to apply two solution for this:

First solution for pages with just one rich:messages;

My rich:messages tag just have globalOnly="true" and there is no for attribute;

   <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>

And my bean send message with 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);
    }

Second solution for pages with more than one rich:messages;

I have added one hidden rich:message to use as a bridge, then my message can have hidden component id and will not be null, after I just pick it up by my rich:messages using this id.

<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>

And I have added message as below via 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文