仅在显示特定h:消息时显示JSF元素

时间:2013-09-16 19:15:40

标签: jsf-2

我有一个h:form有几个输入,每个都有自己的h:message,我正在寻找一种展示方式(使用render或指定一些styleClass })仅在显示特定h:message时显示其他元素(并在未显示h:message时隐藏)。

这是一个代码段

<li>
    <h:panelGroup id="current_password_wrapper">
        <p:password value="#{myBean.myCurrPass}" id="current_password" required="true"
            styleClass="required_field" />
    </h:panelGroup>
    <h:message for="current_password"/>
</li>
<li>
    <h:panelGroup id="new_password_wrapper">
        <p:password value="#{myBean.myNewPass}" id="new_password" required="true"/>
    </h:panelGroup>
    <h:message for="new_password"/>
    <h:commandLink value="my value"/>
</li>

我想仅在显示h:commandLink时才能看到<h:message for="new_password"/>

到目前为止我找不到任何东西......

2 个答案:

答案 0 :(得分:4)

如果您的环境支持EL 2.2,那么您可以检查特定客户端ID的FacesContext#getMessageList()是否不是empty

<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not empty facesContext.getMessageList(new_password.clientId)}" />

如果消息显示为验证错误,那么您还可以检查与消息关联的组件的UIInput#isValid()状态。

<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not new_password.valid}" />

请注意,手动向上下文添加faces消息不会将输入组件标记为无效。因此,应使用真Validator引发ValidatorException,或明确input.setValid(false)调用必须以编程方式完成。

答案 1 :(得分:2)

我认为对于这个问题的答案,您的要求可以存档:

How to number JSF error messages and attach number to invalid component

我认为你可以这样做:

<h:outputText value="output text" rendered="#{bean.messageIndexes['form:input1'] > 0}" />