Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a JSF message added to p:messages if the client id doesn't match?

Tags:

jsf

primefaces

Assuming I have a

<h:form>
    <p:commandButton value="Add no message">
        <p:ajax listener="#{backingBean0.action0()}"
                update="@form"/>
    </p:commandButton>
    <p:messages id="messages"/>
</h:form>

and I provide the wrong client id to FacesContext.addMessage (imagine a typo in the real world):

@Named
@ViewScoped
public class BackingBean0 implements Serializable {
    private static final long serialVersionUID = 1L;

    public void action0() {
        FacesContext.getCurrentInstance().addMessage("inexisting",
                new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "summary",
                        "detail"));
    }
}

Why is the message created in BackingBean0.action0 added to the p:messages? I'd expect the message to be not added because a non-null and non-empty client id has been specified which doesn't match any p:messages on the client side[1].

Messages seem to be added to the p:messages element matching client id (and only to them) reliably. The Javadoc of FacesContext.addMessage states

Append a FacesMessage to the set of messages associated with the specified client identifier, if clientId is not null. If clientId is null, this FacesMessage is assumed to not be associated with any specific component instance.

Parameters:

clientId - The client identifier with which this message is associated (if any) message - The message to be appended

Throws: IllegalStateException - if this method is called after this instance has been released NullPointerException - if message is null

which, of course, raises the question, what a non-associated message could mean (is it displayed or not or queued somewhere - association is just a word with zero explanatory force in this context).

And then there's

The client identifier with which this message is associated (if any)

which in my interpretation means that if there's no client id match the message isn't displayed, but again, that strongly depends on what association means in this context.

This question is obviously not focused on solving the issue which is done by specifying a correct client id, but to understand this.

I experienced this with Primefaces 6.2.


[1] Actually, I'd expect an exception to be thrown because that's way more elegant and time-waisting-save, but that might be bad idea for reasons I don't see.

like image 242
Kalle Richter Avatar asked Oct 17 '25 22:10

Kalle Richter


1 Answers

Let me try and clear this up for you. Whenever you add a FacesMessage it always gets added to the context and the client-id is just a helper to determine "which" component you want this validation associated with.

PrimeFaces offers some help on their messages component in the form of the globalOnly flag.

globalOnly = When true, only facesmessages without client-ids are displayed. Default is false.

This way you can have at the top of your page a way to display global messages and validation messages separately. The client-id will only highlight a component in red also if the client-id matches a real one.

<p:messages id="globalMessages" globalOnly="true"/>

<p:messages id="validationMessages" globalOnly="false" redisplay="false"/>

Definitely check out the Messages demo page to see messages in action: https://www.primefaces.org/showcase/ui/message/messages.xhtml

like image 79
Melloware Avatar answered Oct 22 '25 05:10

Melloware



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!