2

Moin!

I have a situation where I try to display an error message using p:message for an Exception that is raised in a getter. But this results into the message "WARNUNG: There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered.".

I do the following to attach the Message to FacesContext:

FacesContext.getCurrentInstance().addMessage(cliendId, getFacesMessage(severity, msgKey, args));

I think the message cannot be rendered, because, creating a message in a getter happens in a phase where it is to late to render it...

Is there a way to create a message in a getter instead of in an action? Maybe it is possible to reset the rendering phase?

treeno
  • 2,510
  • 1
  • 20
  • 36
  • What's the functional requirement? Why don't you use a `Validator`? – BalusC Jan 12 '12 at 12:31
  • I need to load data from a foreign system. I acccess the foreign server in the bean-getters. I want to show an errormessage in case of a networkerror. I think a Validator can only be used when values are set in the bean (Validation Phase), not read from the bean... – treeno Jan 12 '12 at 15:07
  • Displaying messages is usually associated with validation. But now the functional requirement is more clear, you indeed don't need validation at all. – BalusC Jan 12 '12 at 15:37

3 Answers3

4

I need to load data from a foreign system. I acccess the foreign server in the bean-getters. In case of an networkerror, I want to show an errormessage

Doing the business job in a getter method is the wrong approach. In JSF managed beans, a getter should not do anything else than just returning the data or at least to do some precalculations on the already-available properties. You should not assign property values inside a getter method, or it must be for lazy loading.

The getter is invoked during rendering of the view. In your particular case, the getter is aparently invoked after the <p:messages> is been rendered and thus it never gets the chance to show the faces message which is been added later on.

Move the business job to the (post)constructor or an (action)listener method of the backing bean instead.

E.g. in postconstruct method:

@PostConstruct
public void init() {
    try {
        data = loadFromWebservice();
    } catch (SomeException e) {
        log(e);
        addFacesMessage();
    }
}

You could of course also move the <p:messages> to the very bottom of the page (and if necessary reposition it using CSS), but that's more a workaround than a solution.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The link on JSF calling getters multiple times is particularly relevent for this question! – Jeremiah Orr Jan 12 '12 at 17:26
  • Thanks for your advice. I had to refactor that as you recommendet. But I was not able to use @PostConstruct because I have a Bean with ConversationScope. I used f:event type="preRenderView" instead. – treeno Jan 13 '12 at 15:49
1

p:message says you're using PrimeFaces. It's probably an AJAX call and p:message component is not updated at this call. It's a common error.

Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
  • I also tried it with h:message and it didn't work either... When this is a common error, is there also a common solution for this? – treeno Jan 12 '12 at 12:29
  • Peter Gwiazda means that you need to rerender p:message with the ajax call (if this is an ajax call). Changing to h:message won't help. – Pablo Jan 12 '12 at 12:44
  • I don't think that this is an ajax request because it happens when I load the page – treeno Jan 12 '12 at 13:01
  • Do you have attribute "for" set properly to the clientId that causes problem? Try to add p:messages component to try if it catches the message. – Piotr Gwiazda Jan 12 '12 at 13:33
  • Yes, I've used the "for" attribute. p:messages doesn't show the message neither. – treeno Jan 12 '12 at 13:46
  • This question/answer has the answer for my implementation. I was not setting p:ajax update="..." correctly, so I did update=":formID ..." and it solves this issue for me. Thanks! – Howard Dec 27 '12 at 06:12
  • Clarification: I was not including p:messages in p:ajax update="...", so I did update=":formID ..." (where 'formID' contains p:messages) and it solves this issue for me. Thanks! – Howard Dec 27 '12 at 14:11
0

alternative you can use <p:growl> for show you FacesMessages, too.

<p:message> works analog to <p:growl>.

Both, <p:message> and <p:growl> are able to show specific FacesMessages. Check always your severity settings.

If you use for example:

<p:growl id="growl" showDetail="true" severity="info,warn" sticky="false"/>
<p:growl id="errorGrowl" showDetail="true" severity="error" sticky="true" />

generate FacesMessage with an severity error and put it to false component (growl), you will get the message "WARNING: There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered."

The right way to show your errormessage is:

public void saveError( FacesMessage facesMessage )
{
    FacesContext.getCurrentInstance().addMessage( "errorGrowl", facesMessage );
    update( "errorGrowl" );
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95