This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes display message when page is first displayed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "display message when page is first displayed" Watch "display message when page is first displayed" New topic
Author

display message when page is first displayed

Jorge Fiallega
Greenhorn

Joined: May 27, 2005
Posts: 12
Hello everyone.

The problem I have been trying to solve is how do you display a global message when you first call a page. What I mean is that when you request a page, I want to process some business logic and depending on the result display or not display some error messages together with the rest of the page. I have tried to put the logic in the constructor of the backing bean (pagecode bean) but the first time around nothing gets displayed

FacesContext objContext = FacesContext.getCurrentInstance();
objContext.addMessage(null, objMessage);

Is that because at that point, the first time the page gests called the JSF context does not really exist so the messages don't get queued?

If you happen to have a a "dummy" button on the page that navegats back to the page, now the code in the constuctor of the backing bean seems to get the right FacesContext, store the message and latter when rendering the page is displayed correctly.
I was researching the problem and I read a different message where it explains the first time the page is called, by the time the contructor of the backing bean gets called is already in the Render Response Face, which really does not make any sense to me.

Again the question is how to display error messages in the messages tag
(<h:messages styleClass="message" globalOnly="true" layout="table"></h:messages> the first time you load the page.

Thaks

Jorge
Jorge Fiallega
Greenhorn

Joined: May 27, 2005
Posts: 12
I have found a few more things but I still can't put everything together.

1. If I queue the message in an actionListener the message shows after I push the button, but if I queue it when calling the getter of a bound property then the message is queued but not displayed. The only explanation I can find for this behaviour is that the actionlistener gets executed in the invokeApplication face, while the getter of the bound property is executed at rendering time and although it queues the value, the rendering of the message tag is already done, so it will not contin the new value.

If I queue the message in the constructor of the managed bean it does not show the first time but it shows the second time. The only explantion I can find is that the first time the page is requested the bean is instantiated after the message tag has been rendered. The second time the page gets loaded the constructor is instantiated prior to the rendering so it shows up.

Can anybody through some light? and the question from the original posting still stands

Thanks

Jorge
Tim Fulmer
Greenhorn

Joined: Apr 03, 2006
Posts: 1
Hi Jorge,

I'm running into the same thing while attempting to catch an empty table display with an intelligent message instead of displaying a dumb empty table.

Did you find a way to do this nicely within the JSF framework?

The only things I can think of are a bit hacky:

a.) Create a custom h:messages component which does a hard-coded check for empty table and stuffs in an additional empty table message before delegating to super;
b.) Put the same functionality into a custom tag which interacts with h:messages through FacesContext;
c.) Come up with some funky generalized tag that accepts an I18N key and a bit of JSP-EL describing what to check for being empty;
d.) Some combination of the above.

While something like option C sounds best I just can't believe JSF has this problem. You'd think that being abstracted from any template language like JSP would imply abstraction from the top-down parsing JSP performs.

Anyway, $.02.

--
Tim


Originally posted by Jorge Fiallega:
I have found a few more things but I still can't put everything together.

1. If I queue the message in an actionListener the message shows after I push the button, but if I queue it when calling the getter of a bound property then the message is queued but not displayed. The only explanation I can find for this behaviour is that the actionlistener gets executed in the invokeApplication face, while the getter of the bound property is executed at rendering time and although it queues the value, the rendering of the message tag is already done, so it will not contin the new value.

If I queue the message in the constructor of the managed bean it does not show the first time but it shows the second time. The only explantion I can find is that the first time the page is requested the bean is instantiated after the message tag has been rendered. The second time the page gets loaded the constructor is instantiated prior to the rendering so it shows up.

Can anybody through some light? and the question from the original posting still stands

Thanks

Jorge

[ April 03, 2006: Message edited by: Tim Fulmer ]
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
I use the following



This works for me. Calling the useBean creates the bean prior to entering into the JSF tags. Thus the bean is already in session by the time you call the getter with the <h utputText/>. Just include your setup and check code inside the constructor of the bean.
Ian Springer
Greenhorn

Joined: Feb 25, 2009
Posts: 1
And if using Facelets, then you just need to include an EL expression that references the managed bean somewhere on the page above the location of the h:messages tag.
Pari Gandhi
Greenhorn

Joined: Feb 26, 2009
Posts: 10
you could use a phase listener to add the message to the faces context before rendering the page.





allen catadman
Greenhorn

Joined: Apr 16, 2009
Posts: 7
do it in phaselistener beforePhase

or hx:scriptCollector preRender

<hx:scriptCollector preRender="#{myBean.init}">


it will not work if you addMessage using the managed bean constructor

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: display message when page is first displayed
 
Similar Threads
Displaying component id for error message
h:messages prints error message twice on first submit
button inside a h;messages
f:selectitems issue in jsf
display of multiple messages