Wednesday, August 6, 2014

How to call a method during various JSF lifecycle phases

In order to invoke a method during various lifecycle phases on JSF, you can either use JSF 2.0 <f:viewAction>tag. or <f:event> tag.
The functioning of both tags is very similar, both need a definition for trigger mechanism(when to invoke method) and action mechanism(which method to invoke). These tags are defined in in the meta definition section of the page as follows

<f:viewAction>

This action component specifies an application-specific command (or action), using an EL method expression, to be invoked during one of the JSF lifecycle phases,

<f:metadata>
  <f:viewAction phase="PROCESS_VALIDATIONS" action="#{person.init()}"/>
</f:metadata>

The attributes "action" and "phase" define which method to invoke for an event.
 action: EL method expression i.e. method to invoke. e.g.#{person.init()}
 phase: Name of the event for which to install a listener.


The above tag will result in person.init() method invoked before the "Process Validation" JSF lifecycle phase.

The init method (in the "Person" BackingBean) can be as follows:

public String init() {      
  // do something like get List of persons from database
}

The "phase" attribute can have one of the possible values.

  1. APPLY_REQUEST_VALUES
  2. PROCESS_VALIDATIONS 
  3. UPDATE_MODEL_VALUES 
  4. INVOKE_APPLICATION 

The default is INVOKE_APPLICATION.


<f:event> 

This specifies an application-specific action, using an EL method expression, to be invoked during one of the JSF lifecycle phases.

<f:metadata>
  <f:event type="preRenderView" listener="#{person.init()}"/>
</f:metadata>

The attributes "listener" and "type" of <f:event> define which method to invoke for an event.
 listener: EL method expression i.e. method to invoke. e.g.#{person.init()}
 type: Name of the event for which to install a listener.

As a result of the above <f:event> declaration, JSF run-time invokes person.init() prior to rendering the components.

The init method (in the "Person" BackingBean) is shown as follows:

public void init() {        
  // do something like get List of persons from database
}


The "type" attribute can have one of the possible values.
  1. preRenderComponent
  2. preRenderView
  3. postAddToView
  4. preValidate  
  5. postValidate


<f:viewAction> differs from <f:event> in the following manner


  • View actions, by default is not invoked in post-backs; where as in f: event, post-backs needs to be programmatically checked.
  • View actions have navigation capabilities, View actions support both implicit and explicit navigation.  (action method returns backs a view or in xml), where as in f:event one needs to manage navigation programmatically using response.sendRedirect or using NavigationHandler
  • View actions can be triggered early on, before a full component tree is built, resulting in a lighter weight call.




20 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice Article. We are authorised support partner in Quickbooks Payroll. If you required any help for Quickbooks Payroll Support Phone Number 1-800-986-4607 if you are Expand your business to a new hike, with progressive approach. Seeking for the best accounting software? Then, get quickbooks installed in your system. The software proves to be more profitable to the business

    ReplyDelete
  3. The difference between each stage is very important, click here for more info about the qualities of the reworder so you should definitely understand at what stage and how you can use this data.

    ReplyDelete
  4. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Selenium online training
    Selenium certification training
    Selenium online course
    Selenium training course

    ReplyDelete
  5. This article is truly essential and detailed, since I was able to get what I wanted to! Appreciate your shared educational lecture!

    ReplyDelete
  6. We have a panel of QuickBooks Proadvisor Support Phone Number experts who can help you to solve your QuickBooks related issues. The solutions we deliver will take only few seconds of you. We have the availability 365 days a year and 24 hours a day!

    ReplyDelete
  7. Resurge is absolutely 100% natural, safe and effective. Many thousands of folks enjoy taking Resurge every day and there has been absolutely zero side effects reported. Every capsule of Resurge is manufactured here in the USA in our state of the art FDA approved and GMP (good manufacturing practices) certified facility under the most sterile, strict and precise standards. Resurge is 100% all natural, vegetarian and non-GMO. As always, if you have a medical condition it's recommended to consult with your doctor. Best natural foods for weight loss

    ReplyDelete
  8. "Nice post! Thanks for sharing valuable article.
    Please Visit our Website supply chain
    Incoterms"

    ReplyDelete
  9. What an extremely wonderful post this is. Genuinely, perhaps the best post I've at any point seen to find in as long as I can remember. Goodness, simply keep it up.

    360DigiTMG

    ReplyDelete


  10. Somebody Sometimes with visits your blog normally and prescribed it as far as I can tell to peruse too.

    hrdf claimable

    ReplyDelete
  11. I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
    360DigiTMG data science malaysia

    ReplyDelete
  12. incredible article!! sharing these kind of articles is the decent one and I trust you will share an article on information science.By giving an organization like 360DigiTMG.it is one the best foundation for doing guaranteed courses
    data science course noida

    ReplyDelete
  13. Very informative post ! There is a lot of information here that can help any business get started with a successful social networking campaign !
    Data Science Training in Hyderabad

    ReplyDelete
  14. Who Should Undergo Fut Hair Transplantation
    Superb exertion to make this blog more awesome and appealing.

    ReplyDelete
  15. HVAC & Plumbing Services
    Air Star Heating guarantees reliability and quality for all equipment and services.

    Air Star Heating specialists always try to deliver the most excellent quality of services to our customers at an affordable price. It is understood that every client has different needs and different problems. We try to accomplish the needs of every client according to their requests. We are having considerable experience in this field. Our specialists understand very well how things work. It doesn’t matter in which field of industry you are looking for services.
    Plumbing & HVAC Services in San Diego. Call now (858) 900-9977 ✓Licensed & Insured ✓Certified Experts ✓Same Day Appointment ✓Original Parts Only ✓Warranty On Every Job.
    Visit:- https://airstarheating.com

    ReplyDelete
  16. Play the Best Slot Machines for Real Money - DrmCD
    Play free casino slots for real money right 김천 출장마사지 here on DrmCD. Play 안동 출장안마 with best bonuses and free spins. Discover how to 울산광역 출장샵 win on slots, 과천 출장마사지 scratch cards, 문경 출장마사지

    ReplyDelete
  17. Thank you so much for sharing such an amazing information with us. Visit for GeM Helpdesk Helpline, Tender Services, OEM Panel on GeM, and Tender Consultancy in Delhi NCR. Visit our website for more information in details.
    Gem Consultancy in Delhi NCR

    ReplyDelete