Parameter Passing / Request Parameters in JSF 2.0

This Blog is a compilation of various methods of passing Request Parameters in JSF (2.0 +)

(1)  f:viewParam

One of the features added in JSF 2.0 is "View Parameters"; Simply speaking it allows adding "Query string" or "Request Parameter" to the intended URL. One of the benefits is that it facilitates URL bookmarking.

In JSF 2.0 "View Parameters" is implemented by UIViewParameter. This class extends UIInput (jee6 api docs) and hence inherits features like Converter's, Validator's, full JSF lifecycle support, value binding and listeners.

"View Parameters" are defined in a Facelet page using <f:viewParam> tag. This tag is used inside metadata facet of a view <f:metadata>, which causes a UIViewParameter to be attached as metadata for the current view.
(source index.xhtml)


The above tag/config tells JSF engine to bind the value of "day" request parameter from the url (the url would be something like http://hostname/index.xhtml?day=Sunday) to the "day" property of "viewParamManagedBean". As mentioned above, "UIViewParameter" extends "UIInput", so it inherits all attributes like "required", when required="true", if the url does not include the required param, the page is not displayed.

This example attaches an component event listener (source page2.xhtml)

 Its noteworthy that "setDay()" method of BackingBean is called in update model value phase, so its not available in @PostConstruct and so as a result to do initialization/preloading based on the set value, you need event listener as described above.

You can also include the view params in the links by simply using attribute includeViewParams="true"


You can also use "includeViewParams" in backing bean to include viewParams in the navigation link as follows


(2) f:param

"View Params" is a good way of passing along parameters i.e. parameter that your page receives. However to send a parameter from a facelet to backing bean, or that matter to any other facelet you can use following approach:

and in the backing bean declare a @ManagedProperty and link its value to the request parameter by


 
To send a parameter to another JSF page




In the managed bean of page2, you can declare a managed property (as shown in example above) and capture value of passed in parameter.

(3) Method Expression

You can send parameter as an argument to an action method i.e. bean.actionMethod(Type param1, Type param2)
 Here is a sample JSF code with datatable displaying day's of a week, and a select button in each row. The action method is passed an argument, which is the declared variable of the datatable:


Backing Bean:




When you include a f:param tag in “h:commandButton” or "h:commandLink", the parameter is turned into request parameter,  
 
which in the backing bean can be retrieved as
 

(4) f:Attribute

Parameter/Values can be passed to an actionListener method (of a commandUI) using f:attribute
In JSF page



In the Backing Bean capture the value using


One Could also use f:Attribute to pass an attribute to an component. for e.g.
 <h:outputLabel for="sportsPer" value="Do You Play Tennis:" />
  <h:selectOneRadio id="sportsPer" value="#{someBackingBeanView.likesTennis}" validator="#{someBackingBeanView.validatePlayer}">
    <f:selectItem itemLabel="No" itemValue="N" />
    <f:selectItem itemLabel="Yes" itemValue="Y" />
    <f:attribute name="clubNameAttr" value="#{clubName.submittedValue}" />
</h:selectOneRadio>
Where "clubNameAttr" is the bind id of a form field whose value is required in the validator method (multi/complex form field validator).
<h:inputText id="clubNameId" binding="clubName" value=" #{someBackingBeanView.clubName}"/>
Now in the validator method, one can get the value of attribute "clubNameAttr"

public void validatePlayer(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String clubNameAttr = (String)component.getAttributes().get("clubNameAttr");
//do something ...
}

(5) f:setPropertyActionListener

Parameter/Values can also be passed to an actionListener method (of a commandUI) using f:setPropertyActionListener
In JSF page

 In the Backing Bean

One thing to note is that the setDay() is called after the listener method (listenerToGetAttributes(..)) completes. So if the scope of "DayOfWeekManagedBean" is higher than Request, then you will see that the value of "day" in listener method is of the previously clicked button and NOT the current (because listeners are invoked in the sequence they are defined).

(6) JSTL tag <c:set>

There is no equivalent tag for <c:set>in JSF, f:param is NOT the same. This one comes in handy when f:param is not available in backing bean; for example when passing a parameter to <ui:include> tag. If you add <f:param> for example <f:param  name="activeIndex" value="0"/>, the value will be available in the included JSF page as #{activeIndex}, but NOT in the constructor of the backing bean of the included page. In such scenario, one can use <c:set>


In the Backing Bean capture the value using
 


Prasanna Bhale

56 comments:

  1. This page really helps! Thanks a lot!

    ReplyDelete
    Replies
    1. Great Article Cloud Computing Projects

      Networking Projects

      Final Year Projects for CSE

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. ThankQ very much.Really very useful..

    ReplyDelete
  3. Great article, keep on good work, thnx

    ReplyDelete
  4. Really, really great article.
    You have answered one of the most basic questions about JSF that is no where to be found!
    Somehow this is a very grave issue in JSF.

    ReplyDelete
  5. very nice , exelent !

    ReplyDelete
  6. great and clear tutorial. After hours of reading I finally understood and could actually do something!

    ReplyDelete
  7. Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

    ReplyDelete
  8. Excellent…Amazing…. I’m satisfied to find so many helpful information here within the put up,for latest php jobs in hyderabad. we want work out extra strategies in this regard, thanks for sharing.

    ReplyDelete
  9. Very nice!; which use each case of use?. And why?. Thanks so much!.

    ReplyDelete

  10. شركة الصفرات للتنظيف بالرياض
    Cleaning is one of the priorities of life that is indispensable and must be taken care of permanently on availability in the place So Safrat cleaning company in Riyadh to provide a distinctive set of detergents strong and safe and able to handle all types of stains and remove easily and without the need to bother and hardship cleaning
    https://www.cleanriyadh.com/شركة-الصفرات-للتنظيف-بالرياض/


    شركة الصفرات لمكافحة الحشرات بالرياض
    Al Safrat Pest Control Company in Riyadh provides a range of insecticides that are capable of completely eradicating all types of insects present in the place. All pesticides are completely safe for health and can be treated in the presence of family members normally through the best prices
    https://www.cleanriyadh.com/شركة-الصفرات-لمكافحة-الحشرات-بالرياض/


    شركة الصفرات لنقل الاثاث بالرياض
    Al Safarat Furniture Company in Riyadh offers a very distinctive collection of cars that are used to transport various pieces of furniture and equipped inside to receive all the pieces to be transported properly without any impact on the other
    https://www.cleanriyadh.com/شركة-الصفرات-لنقل-الاثاث-بالرياض/


    شركة الصفرات لتنظيف الخزانات بالرياض
    Al-Safrat Company for cleaning tanks in Riyadh advises its customers with a number of different advice to ensure the safety of the tank and the health of the customer in the first place
    And on top of these tips
    Do not leave the tank filled with water if not in use
    https://www.cleanriyadh.com/شركة-الصفرات-لتنظيف-الخزانات-بالرياض/

    ReplyDelete

  11. شركة تنظيف بالمدينة
    A cleaning company in Madinah provides a very large range of the best types of detergents that are completely safe for health and can be dealt with in the presence of family members without affecting them and has a very distinctive result
    https://www.cleanriyadh.com/cleaning-company-in-medina/


    شركة تنظيف بينبع
    A cleaning company recommends that the house should be constantly cleaned and ventilated as there is no room for the spread of unpleasant odors or the spread of various insects. A cleaning company recommends exposing all places in the house to direct sunlight and heat in order to kill any delicate insects in the place
    https://www.cleanriyadh.com/cleaning-company-yanbu/

    شركة نقل اثاث بينبع
    Moving furniture is not easy to work on without any problem so a furniture transfer company in Yanbu provides a distinguished group of carpenters who are able to dismantle and install all kinds of furniture to be transferred properly
    https://www.cleanriyadh.com/furniture-moving-company-yanbu/

    ReplyDelete
  12. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing...
    Blockchain online training

    ReplyDelete
  13. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.half day leave application for urgent work

    ReplyDelete
  14. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article. best blockchain online training

    ReplyDelete
  15. Rebel wilson has started her weight loss journey in 2017 and lost around 20 pounds of weight. If you want to know more about rebel wilson weight loss journey then check out our website.

    ReplyDelete

  16. That is nice article from you , this is informative stuff . Hope more articles from you . I also want to share some information about cognos analytics 11

    ReplyDelete
  17. i really like the way this article is written, keep up the good work also check out my website for the before buy

    ReplyDelete

  18. This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me. Blockchain training in Chennai.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    blockchain online training
    best blockchain online training
    top blockchain online training

    ReplyDelete
  21. نقل العفش نقل الاثاث نقل العفش والاثاث في المدينة المنورة خدمات تنظيف خدمات التنظيف رش الحشرات مكافحة الحشرات خدمات التنظيف بالمدينة المنورة مؤسسة نقل العفش في المدينه المنوره غسيل الخزانات تنظيف الخزانات بالمدينة المنورة , النظافة العامة , خدمات تنظيف المدينة المنورة

    شركة نقل عفش بالمدينة المنورة
    شركة نقل اثاث بالمدينة المنورة
    شركة مكافحة النمل الابيض
    مكافحة حشرات بالمدينة المنورة
    شركة رش مبيدات بالمدينة المنورة
    شركة نظافة بالمدينة المنورة
    شركة تنظيف شقق ومنازل بالمدينة المنورة

    ReplyDelete
  22. Hey!Amazing work. With full of knowledge. Our Team resolve any glitches occurring while utilizing the software. Looking for QuickBooks Customer Service Contact us +1-855-756-1077.Our experts will assist you to fulfil your accounting needs. The solutions are accurate and time-saving.

    ReplyDelete
  23. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    blockchain online training
    best blockchain online training
    top blockchain online training

    ReplyDelete
  24. Hey! Fabulous post. It is the best thing that I have read on the internet today. Moreover, if you need instant support for QuickBooks, visit at QuickBooks Customer Service Phone Number Our team at QuickBooks Customer Service is always ready to help and support their clients.

    ReplyDelete
  25. Hey! Mind-blowing blog. Keep writing such beautiful blogs. In case you are struggling with issues on QuickBooks software, dial QuickBooks Phone Number. The team, on the other end, will assist you with the best technical services.

    ReplyDelete
  26. صباغ الكويت افضل خدمة يحتاجها اهل الكويت فى الوقت الحالي حيث انه من النادر ان تجد صباغ بالكويت محترف ولكن اليوم فى موقع صباغ الكويت سوف تجد افضل صباغين وافضل ديكورات الكويت على احدث صيحه لعام 2021 مثل ورق جدران مودرن بالكويت  ويوجد لدينا تركيب باركية خشب بالكويت ومع ارتفاع الاسعار يمكنك ايضا تركيب بديل الرخام بالكويت وهناك موديلات جديده مثلا استيل الكويت  ناتي الى افضل صباغين بالكويت هنالك جنسيات مختلفة مثل صباغ مصري و صباغ باكستاني وايضا هنالك صباغ هندي جميع الصباغين على اعلى مستوى وبالاخص صباغ عراقي  ونقوم بتقديم الخدمات فى جميع مناطق الكويت مثل صباغ الجهراء  و صباغ مبارك الكبير  و صباغ حولي  و صباغ الفروانية بالاضافة الى صباغ العاصمة  و صباغ الاحمدي  جميع تلك الخدمات نقدمها بالاضافة الى صباغ وايضا صباغ بالكويت بالاضافة الى ارخص صباغ و اشطر صباغ و افضل صباغ ولدينا معلم صباغ يوجد لدينا افضل صباغ بالكويت

    ReplyDelete
  27. Thanks for sharing such useful information with us. I hope you will share some more info about your blog. Please keep sharing. We will also provide QuickBooks Support Phone Number for instant help.

    ReplyDelete
  28. very impressive blog thanks for sharing with us if you have problems about QuickBooks Software? you can connect our team at Quickbooks Customer Service+1 855-201-8294

    ReplyDelete
  29. QuickBooks is the most widely used accounting software Are you facing QuickBooks Error ? and want to fix errors and quick solution at Quickbooks Customer Service Number+18557387873

    ReplyDelete