0
votes

In my Xpages application I am using Bootstrap for theming. I have a xp:messages box that I have applied some Bootstrap CSS to.

When then are no messages I want to hide the panel that contains the messages. So I came up with the following:

<xp:panel rendered="#{javascript:facesContext.getMessages().hasNext()}"                 styleClass="alert alert-info">
<xp:text escape="true" value="#{javascript:facesContext.getMessages().hasNext()}">
</xp:text>
<xp:messages globalOnly="true" layout="list"></xp:messages>
</xp:panel>

However I notice sometimes the xp:messages control is empty but facesContext.getMessages().hasNext() returns true.

Is there another way to check the value of xp:messages? I tried getComponent(...).getValue() but than the application breaks.

This is what is returned to the browser:

<div class="alert alert-info">
<span>true</span><span role="alert"></span></div>
3

3 Answers

3
votes

I write based on your example code, that is, you want to deal with global messages only. It's important that you pass a null value to the getMessages method. In that way you will be filtering for the global messages only or, in other words, messages that are not tied to a specific component.

All messages: facesContext.getMessages()

Global messages only: facesContext.getMessages(null)

Messages for specific component: facesContext.getMessages('whateverIdYouHave')

2
votes

have you tried to directly apply the bootstrap class to the xp:messages control? e.g.

<xp:messages id="messages1" infoClass="alert alert-info"></xp:messages>

hereby you do not need the surrounding panel and compute the rendered property

1
votes

You want to wrap that call into a function you keep in a SSJS library and use something like rendered=JavaScript:hasMessages() That makes is easier to understand the code later.

Inside the function you should be able to either iterate over the messages and check if they are empty (or eventually: messages you don’t care about it).

The JSF spec has getMessageList() that returns a Collection you can check with isEmpty(). Not sure if it has been implemented in XPages. Paul, as usual, has more info here:

http://www.intec.co.uk/how-many-xpages-errors-have-i-got/

The counter seems most reliable