This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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...
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.
Joined: Aug 21, 2008
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:
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).
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.