JSF using EL to test global messages presence

后端 未结 3 1586
[愿得一人]
[愿得一人] 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.

提交回复
热议问题