Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF using EL to test global messages presence

Tags:

jsf

I'm trying to display a block only if there are Global messages in the JSF Queue.

I tried to use rendered="#{not empty facesContext.getMessageList(null)}", but it's always evaluated to false.

Only way I found is to create a custom EL function and test it in java.

eg. : my el function :

public static boolean isFacesGlobalMessages() {
  return ! FacesContext.getCurrentInstance().getMessageList(null).isEmpty();
}

JSF page :

<h:panelGroup class="block1" layout="block" rendered="#{el:isFacesGlobalMessages()}">
  <div class="block-warn-body">
    <rich:messages id="msg" globalOnly="true"/>
  </div>
</h:panelGroup>

I'm using Mojarra 2.1.5.

Am I missing something ? Thanks !

Edit : tried the following suggestions, but no luck so far :

  • #{not empty facesContext.getMessageList(null)} -> always false
  • #{! facesContext.getMessageList(null)} -> error
  • #{! empty facesContext.getMessageList(null)} -> always false
  • #{fn:length(facesContext.getMessageList(null)) > 0} -> always false
  • #{not empty facesContext.messageList(null)} -> Error : Method messageList not found
  • #{not empty facesContext.messageList} -> returns true if it's a validation error (I only want true on global error)
  • #{! facesContext.getMessageList(null).isEmpty()} -> throws IllegalAccessException: Class javax.el.BeanELResolver can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public"
like image 487
gonzalad Avatar asked Jul 31 '12 09:07

gonzalad


3 Answers

no need for custom EL function

try this

rendered="#{not empty facesContext.messageList}"

EDIT

Haven't tried it myself , but try

rendered="#{not empty facesContext.messageList(null)}"

An idea...

 rendered="#{not facesContext.validationFailed and not empty facesContext.messageList}"
like image 82
Daniel Avatar answered Jan 04 '23 06:01

Daniel


I know this is an old thread, but after struggling searching for an elusive solution, I found an explanation for this behaviour, and since I cannot find this explanation anywhere (dunno why), I think this could be helpful.

I made an el function slight different:

public static boolean hasMessages(String clientId) {
    return !FacesContext.getCurrentInstance().getMessageList(clientId).isEmpty();
}

The difference is the parameter clientId. The behaviour of this function is exactly the same as using #{not empty facesContext.getMessageList(clientId)}. Debugging the code, I noticed that when I called the function with clientId = null, the value of clientId inside the function actually is "" (empty string).

After that, I consulted the EL 3.0 Spec, and found:

Section 1.23 - Type conversion

Every expression is evaluated in the context of an expected type. The result of the expression evaluation may not match the expected type exactly, so the rules described in the following sections are applied. [...]

Section 1.23.2 - Coerce A to String

If A is null: return "" [...]

So, I don't think that there's a way to request messages with clientId = null passing the null value as a parameter. The only way is to have a function that does that without using a parameter or testing if the parameter was set to the empty string.

like image 42
Luciano Avatar answered Jan 04 '23 05:01

Luciano


Try this:

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

instead of:

rendered="#{not empty facesContext.messageList(null)}"

in the Daniel's answer.

Or this one:

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

where 'inputForm' is:

<h:form id="inputForm">
...
</h:form>

if you want to address it to only one of the several forms on your page.

like image 40
Aleksei Loginov Avatar answered Jan 04 '23 05:01

Aleksei Loginov