0

I've read dozens of question regarding similar problem, but could not find an answer to it. I've got this piece of code inside a form:

<p:outputPanel id="articleInfo">
    <p:growl id="messages" autoUpdate="true"/>
    <p:panel rendered="#{not empty myBB.selectedProduct}">
        <p:panel>
            <h:outputText escape="false" value="#{myBB.content}"/>
        </p:panel>
        <p:commandButton value="Button" update=":mainForm:articleInfo"/>    
    </p:panel>
</p:panel>

When I click a button, new content is generated in the backing bean and FacesMessage is added like this:

public String getContent(){
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, detail));
    return "something";
}

The ajax update is fine because I see new content, addMessage is also called, but I cannot see the message. I've tried p:messages instead of growl, adding for="mainForm:articleInfo" and calling the addMessage with "mainForm:articleInfo" (I've checked that it's the correct ID in the view.

I still cannot find a reason for this.

NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
  • p:commandButton has update property itself why are you using ajax event. – Makky Feb 13 '14 at 12:20
  • It's actually copied from the ```p:tree```, but I wanted to keep the code minimal. if I put it straight on the commandButton update attribute, the outcome is the same. I've changed it so it's not that confusing. – NeplatnyUdaj Feb 13 '14 at 12:22
  • I don't see actionListener or action on cmomand button ?? – Makky Feb 13 '14 at 12:28
  • There is none. Why do I need it? The update works fine and the content is reloaded. – NeplatnyUdaj Feb 13 '14 at 12:29
  • ok this is confusing...who is adding message to the faces context? What actino is doing that ? If possioble, post full cod.e – Makky Feb 13 '14 at 12:32
  • The message is added in the getter when ```#{myBB.content}``` is loaded. I've updated the question. – NeplatnyUdaj Feb 13 '14 at 12:36
  • 1
    This is not ideal. You should not add such things in a getter as it gets called more than ones. http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – Makky Feb 13 '14 at 12:48
  • Ideally, yo'd add such a message on some action. – Makky Feb 13 '14 at 12:49
  • Try to update the `p:panel` only or just the `h:outputText` or put the growl at the end of the panel. I guess the reason that you do not see any message, is that the `h:outputText` is rendered after the `p:growl/messages` when you are updating the `p:outputPanel`. So the messages is added after the update of the message container is complete. – user1983983 Feb 13 '14 at 13:07
  • I've refactored the code so that the message is added on action and not from the getter and now it works. Thanks! – NeplatnyUdaj Feb 13 '14 at 13:11
  • @Makky: Could you add it as an answer so that I can accept it? – NeplatnyUdaj Feb 13 '14 at 13:30

1 Answers1

1

The reason your messages are not displayed because you've added the code to add message in a getter which is not ideal. Why getter gets called multiple times

Remove the adding message code and put inside a method which gets called on some action.

Community
  • 1
  • 1
Makky
  • 17,117
  • 17
  • 63
  • 86