在PrimeFaces中的工具提示上显示错误消息时

在PrimeFaces中的工具提示上显示错误消息时

本文介绍了在PrimeFaces中的工具提示上显示错误消息时,如何摆脱空的工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在<p:tooltip>上的某处显示错误消息,如下所示.

I display error messages somewhere on <p:tooltip> as follows.

<p:inputText id="text" value="#{bean.text}" required="true"/>

<p:tooltip for="text">
    <p:message for="text"/>
</p:tooltip>

尽管在给定的工具提示下显示错误消息,但在没有错误的情况下显示为空/不必要的工具提示,如下图所示-在文本框的右下角旁边.

Although it displays an error message the given tooltip, an empty/unnecessary tooltip is shown, when there is no error as can be seen in the following picture - beside the bottom right corner of the text box.

如何摆脱这种空洞的工具提示? (我以某种方式尝试过,但是没有用)

How to get rid of such empty tooltips? (I tried someway but it did not work)

推荐答案

可以通过检查列表java.util.List<FacesMessage>中的错误消息来完成,该错误消息可以通过使用facesContext.messageList获得.

It can be done by checking for an error message in the list java.util.List<FacesMessage> that can be obtained by using facesContext.messageList.

可以根据在列表中找到的错误消息为<p:tooltip>rendered属性进行设置,该错误消息与沿线的关联组件有关.

The rendered attribute of <p:tooltip> can be set based on the error message/s found in the list for the associated component/s something along the line.

rendered="#{not empty facesContext.getMessageList('clientId')}"

工作代码段:

<h:form id="form">
    <p:panel id="panel">
        <p:inputText id="text" value="#{bean.text}" required="true"/>

        <p:tooltip for="text" rendered="#{not empty facesContext.getMessageList('form:text')}">
            <p:message for="text"/>
        </p:tooltip>

        <p:commandButton value="Submit" update="panel"/>
    </p:panel>
</h:form>


或通过使用组件绑定.例如


Or by using component binding. Such as,

<p:inputText id="text" binding="#{inputComponent}" value="#{bean.text}"/>

<p:tooltip for="text" rendered="#{not empty facesContext.getMessageList(inputComponent.clientId)}">
    <p:message for="text"/>
</p:tooltip>

甚至

<p:inputText id="text" binding="#{inputComponent}" value="#{bean.text}"/>

<p:tooltip for="text" rendered="#{not inputComponent.valid}">
    <p:message for="text"/>
</p:tooltip>

最后两种情况特别有用,特别是当(输入)组件包含在迭代组件(如<p/h:dataTable><p:dataGrid><p:dataList>(甚至<ui:repeat>))中时,其中确定了组件的唯一性基于迭代组件(例如form:dataTable:0:textform:dataTable:1:textform:dataTable:2:text ...等)的迭代行索引

The last two cases are useful especially when the (input) component is enclosed within an iterating component like a <p/h:dataTable>, <p:dataGrid>, <p:dataList> (or even <ui:repeat>) where the uniqueness of enclosing components is determined based on the iterating row index of an iterating component such as, form:dataTable:0:text, form:dataTable:1:text, form:dataTable:2:text... and so on

这篇关于在PrimeFaces中的工具提示上显示错误消息时,如何摆脱空的工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:19