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?

3
  • What's the functional requirement? Why don't you use a Validator? Commented Jan 12, 2012 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... Commented Jan 12, 2012 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. Commented Jan 12, 2012 at 15:37

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

2 Comments

The link on JSF calling getters multiple times is particularly relevent for this question!
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.
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.

7 Comments

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?
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.
I don't think that this is an ajax request because it happens when I load the page
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.
Yes, I've used the "for" attribute. p:messages doesn't show the message neither.
|
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" ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.