There is very common use case when we need to do some preparation work on initial page rendering. For example, we need to do some initialization stuffs like displaying error messages or disabling/enabling tool buttons depending on the model's state and so on. As usual we create some phase listener or implement BeforePhase event of the f:view tag. The issue is getting more complicated for task flows with page fragments. Page fragments don't have the f:view tag.
JDeveloper 11g R2 allow us to resolve the issue in elegant and easy way. JSF 2.0 specification supports new f:event tag. It has two simple attributes: type and listener. We can attach f:event tag to any of the page's components meaning the component listens to the event of the specified type and in case of event firing calls correspondent listener provided by some EL expression in the listener attribute.There are some predefined event types: postAddToView, postValidate, preRenderComponent, preValidate. Let's focus on preRenderComponent event. It fires exactly before rendering the component. In the following example we have commandButton with preRenderComponent event listener. The listener enables or disables the button depending on some conditions:
JDeveloper 11g R2 allow us to resolve the issue in elegant and easy way. JSF 2.0 specification supports new f:event tag. It has two simple attributes: type and listener. We can attach f:event tag to any of the page's components meaning the component listens to the event of the specified type and in case of event firing calls correspondent listener provided by some EL expression in the listener attribute.There are some predefined event types: postAddToView, postValidate, preRenderComponent, preValidate. Let's focus on preRenderComponent event. It fires exactly before rendering the component. In the following example we have commandButton with preRenderComponent event listener. The listener enables or disables the button depending on some conditions:
<af:commandButton text="commandButton 1" id="cb1"> <f:event listener="#{managedBean.listenForEvent}" type="preRenderComponent"/> </af:commandButton>
And implementation of the listener looks like this:
public void listenForEvent(ComponentSystemEvent componentSystemEvent) { UIComponent cb = componentSystemEvent.getComponent(); if (cb instanceof RichCommandButton) ((RichCommandButton) cb).setDisabled(getSomeCondition()); }
Nice post.
ReplyDeleteIn the specific case of rendering or not, I prefer to set the component property using an EL expression which can be a call to the condition method.
But for use there are plenty of use cases for f:event.
Thanks.
Jean Marc
So do I. But there are some cases when EL expression for rendered attribute is calculated a little bit earlier then you actually need. In this case f:event helps.
ReplyDeleteHi Eugene,
ReplyDeleteThanks for the post. Is there an alternative way to call a mathod on component render in 11R1?
Can't bind it on Rendered attribute as it is not called each time the component is shown.
Hi ILya,
ReplyDeleteYour method in rendered attribute would be called, if the component were in partial targets of the request.
Hi Fedor.
ReplyDeleteThanks for your article. I am trying to call Application Module method just before a table is rendered and I am trying your tip. But I don`t find the component
f:event
in my fragment. Do you know why?
JDeveloper 11g.1.2.2.0.
THanks in advantage
Hi Juan!
ReplyDeleteI guess you use jspx page, but this feature is available for facelets pages only.