如何更好地使用JSF h:messages? [英] How to use JSF h:messages better?

查看:141
本文介绍了如何更好地使用JSF h:messages?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用h:messages来传达用户-错误消息和确认消息.显示这两种不同消息的CSS样式是不同的,实际上,我想在确认消息旁边使用一张图片.例如:

My Objective is to use h:messages to convey user - error and confirmation messages.The CSS styles to show these two different messages are different, In fact I would like to use an image beside the confirmation message. for Eg:

<tr> <td><img/></td><td><h:msg></td> </td>.

所以我尝试根据2个不同的客户端ID将消息添加到Faces Context

So I tried to add messages to the Faces Context based on 2 different client ids

<tr>
            <td height="5">
                <h:messages style="color:darkred" id="error_message" />
            </td>
        </tr>
        <tr>
            <td width="89%" class="InfoMsg" align="center">
                <h:messages id="confirm_message" />
            </td>
        </tr>

和在Java层

FacesMessage facesMessage = new FacesMessage(Constants.saveMessageConfirm);
    FacesContext.getCurrentInstance().addMessage(Constants.STATIC_CONFIRM_MSG_CLIENT_ID,  facesMessage);

但是,即使我将消息添加到客户端ID Confirm_message-仅添加到Confirm_message-而不是error_message-消息也以2种不同的样式显示两次(请参见上面的HTML)

But, even if i add messages to client Id confirm_message - and only to confirm_message - and not to error_message - The message is shown twice in 2 different styles (refer the HTML above)

2个问题:

1)这是什么问题?

2)如果我想在第二个tr中显示td内的图像,并在出现确认消息时有条件地显示第二个tr-最好的方法是什么?

2) If I want to show the image inside a td in the second tr and conditionaly show that second tr when confirm messages are present - what is the best way?

谢谢

推荐答案

h:messages显示所有消息,也显示页面中h:message中已经显示的消息.但是,您可以使用globalOnly="true"将其设置为显示具有null客户端ID的消息.

The h:messages displays all messages, also the ones which are already displayed in a h:message in the page. You can however set it to only display messages with a null client ID using globalOnly="true".

<h:messages globalOnly="true" />

您还可以根据 FacesMessage.Severity :

You can also give the message a different style depending on the FacesMessage.Severity:

<h:messages infoClass="info" errorClass="error" />

例如,此CSS隐藏

with for example this CSS which hides the INFO messages and makes ERROR messages red:

.info {
     display: none;
}
.error {
     color: red;
}

您可以使用redisplay="false"告诉它不要通过e.e显示已显示的消息. <h:message>.

You can use redisplay="false" to tell it to not display the already displayed messages via e.e. <h:message>.

<h:messages redisplay="false" />

您只需要确保将其放置在所有其他消息组件之后的 组件树中.如有必要,您可以使用CSS将其重新放置在顶部的某个位置.

You only need to make sure it's placed in the component tree after all those other message components. You can if necessary make use of CSS to reposition it somewhere in top.

请确定

facesContext.addMessage("clientId",  facesMessage);

这会将给定的消息附加到<h:message for="clientId">而不是像您期望的那样附加到<h:messages id="clientId">.

this will attach the given message to a <h:message for="clientId"> not to a <h:messages id="clientId"> as you seem to expect.

这篇关于如何更好地使用JSF h:messages?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆