2

我的问题是我在一个 JSF 页面中有 2 个表单,每个表单都有它的<h:message>or<h:messages>标记。我们知道消息/消息标签会打印任何验证错误,所以会发生什么,假设我将 2 个表单中的任何一个的字段留空,它应该打印“字段不能留空”或某种消息。它给出了这个消息,但它给出了两次,因为有两种形式。所以我在这两种形式中都看到了相同的错误/验证消息。

所以我需要的是<h:messages>or<h:message>标签应该只为它们各自的表格显示一次错误/验证消息!

所以任何帮助将不胜感激!

4

1 回答 1

3

如果您使用的是 JSF 2,那么您可以通过 ajax 提交和更新表单。这允许部分更新视图。

<h:form>
    <h:messages />
    ...
    <h:commandButton ...>
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
</h:form>

<h:form>
    <h:messages />
    ...
    <h:commandButton ...>
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
</h:form>

或者,如果您因为某些不明显的原因不能/不想使用 ajax,或者仍在使用旧版 JSF 1.x,请检查是否已提交rendered所需表单的属性。<h:messages>

<h:form binding="#{form1}">
    <h:messages rendered="#{form1.submitted}" />
    ...
    <h:commandButton ... />
</h:form>

<h:form binding="#{form2}">
    <h:messages rendered="#{form2.submitted}" />
    ...
    <h:commandButton ... />
</h:form>

顺便说一句,不应该有这个<h:message>问题,这与你在问题中暗示的相反。

于 2012-10-26T11:11:13.023 回答