JSF using EL to test global messages presence

后端 未结 3 1564
[愿得一人]
[愿得一人] 2021-01-02 15:25

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)}\"

相关标签:
3条回答
  • 2021-01-02 15:41

    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.

    0 讨论(0)
  • 2021-01-02 15:46

    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.

    0 讨论(0)
  • 2021-01-02 15:54

    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}"
    
    0 讨论(0)
提交回复
热议问题