aspose file tools*
The moose likes JSF and the fly likes Events on lifecycle phases Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Events on lifecycle phases" Watch "Events on lifecycle phases" New topic
Author

Events on lifecycle phases

Victor Dolirio
Ranch Hand

Joined: Aug 21, 2008
Posts: 57
It's possible to call a method of backingbeans, triggered by arbitrary events on lifecycle of request? i.e. I want to call one method on my backingbean when restore view phase has been done.

My intention is to update components state, behaviour or fashion (in my backingbeans), on the startup of my pages, depending on model state. i.e. Set some css style on certain components if business rules require it when use open a page.

Currently, I'm picking these behaviours in set and get methods of my backingbeans... Some help?

best regards,


If happen something bad in you life, don't ask God why it happened. Ask for what.
SCJP 5 (80%) - SCWCD 5 (91%) - SCBCD & SCJD coming soon...
Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 95
In JSF, what you are attempting to do (I believe) is a Phase Listener. Although it's possible to perform the work in a backing bean by implementing the listener interface, it's more commonly done in a separate class that implements the interface. Phase Listeners can be invoked before or after any phase...in your case, you would want to invoke it after the RESTORE_VIEW phase.

That being said, I suspect that your application is attempting to do something that may be easier without the phase listener. You were referring to "updating the components state, behavior or fashion (in my backingbeans) on the startup of my pages". A lot of that can be done by just using the lifecycle in the way in which it was designed. For an excellent description of the work that occurs automatically in the different lifecycle phases, see JavaServer Faces: The Complete Reference. Although the index is rather poor, the first chapters are a very good tutorial on this topic.

As an example (and off the top of my head...there might be a different/better way to do this): you can include markup using the <f:verbatim> tag, which can be rendered or not depending on the state of backing bean properties. This would give you the ability to specify different CSS for different runtime conditions. And often, there is no need to directly interact with the component tree and the values contained therein...again, depending on your application.
Victor Dolirio
Ranch Hand

Joined: Aug 21, 2008
Posts: 57
Hi Tom, thanks, this was just what I wanna hear. I hate to do complex actions to accomplish simple requirments. I'll be more thankful if help me to understand what you say with an example.

I have a entity named "Event". This entity have a datetime to the begin and other to the end of the event. If user open the view page of this event just in the time this event is happening, I need to put a style in the label of the caption of the event, as follows:

CSS style:


code snippet for the Page:


What I would do to apply the css class if event is happening just in the time the user open this page? (don't worry with logic of dates, I just wanna know what to do with JSF).

Best regards,
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
Hi Victor,

This can be accomplished with very little effort. Suppose your event object has a boolean property happening, you could write the following code:

That's all.

Best regards,
Bart


SCJP 1.4 | SCJD 1.6 | Visit my website | Author of the book Apache MyFaces 1.2 Web Application Development
Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 95
Bart beat me to it! Exactly what I was going to suggest...use the "style" attribute (common to all UI component tags), and have that point at an EL expression that returns the appropriate style.

The property Bart was referring to would simply determine if the current time was within the range of "begin" to "end". If so, it returns "true"...if not, it returns "false". The style would therefore dynamically change according to runtime conditions.
Victor Dolirio
Ranch Hand

Joined: Aug 21, 2008
Posts: 57
Nice! It's exactly what I need! Simple and agile.

Thanks all you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Events on lifecycle phases