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

43 comments:

  1. This page really helps! Thanks a lot!

    ReplyDelete
  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. Very nice!; which use each case of use?. And why?. Thanks so much!.

    ReplyDelete

  8. شركة الصفرات للتنظيف بالرياض
    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

  9. شركة تنظيف بالمدينة
    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
  10. 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
  11. 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
  12. 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
  13. i really like the way this article is written, keep up the good work also check out my website for the before buy

    ReplyDelete

  14. 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
  15. This comment has been removed by the author.

    ReplyDelete
  16. 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
  17. 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
  18. 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
  19. Hey! What a wonderful blog. I loved your blog. QuickBooks is the best accounting software, however, it has lots of bugs like QuickBooks Error. To fix such issues, you can contact experts via QuickBooks Customer Service Number (855)587-4968.

    ReplyDelete
  20. Hey! Fabulous post. It is the best thing that I have read on the internet today. Moreover, if you need instant support for QuickBooks Error, visit at QuickBooks Customer Service (855)444-2233. Our team is always ready to help and support their clients.

    ReplyDelete

  21. The fact that this software also faces a number of operations during operation is also a thing that can’t be denied. Out of the multiple QuickBooks error, QuickBooks Error Code H202 is also an error that the users often face whenever they try to switch on multi-user mode. Let’s start discussing this common error of QuickBooks and how it can be resolved effectively to avoid any disturbance in operation.

    In this post, we are enlisting quick and easy steps that can help you to troubleshoot the Troubleshoot QuickBooks Sync Manager Error easily. So, read this post till the very end to find the best solution that can fix your QuickBooks Error. Being one of the best accounting software, Quickbooks is used around the Globe by various small and big business enterprises. Facing some major and minor issues with this software is something very common, and its Error can be resolved easily.

    Quickbooks is one of the most popular accounting software among the accountants of big and small businesses. Often people wonder that what is so good about Resolve QuickBooks Script Error to make it so popular, then let us inform you that it’s an all-in-one solution for accounting and finance.

    quickbooks error code h202
    quickbooks sync manager error
    quickbooks script error

    ReplyDelete
  22. Hey! Excellent work. Being a QuickBooks user, if you are struggling with any issue, then dial QuickBooks Customer Service. Our team at QuickBooks will provide you with the best technical solutions for QuickBooks problems. you can contact us at.+1 888-210-4052,PA.

    ReplyDelete
  23. حديقة حيوان الرياض أو Riyadh Zoo عبارة عن أدغال أفريقية على أراضي المملكة العربية السعودية حيث تحتوي على الكثير من الحيوانات بمختلف الأنواع والجنسيات والتي تأتي  حدائق السعودية من مختلف قارات العالم فضلا  عرب دار عن أندر أنواع الحيوانات والتي شارف أغلبها على الإنقراض من افضل حدائق السعودية التي تجذب العديد من الناس

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

    ReplyDelete
  25. I would like to take this chance to thank you for the time and effort you put into crafting this blog piece specifically for me and I would like to do so by taking advantage of this opportunity. I can't express how grateful I am. how to make a homemade face mask for acne

    ReplyDelete
  26. Great article! Your insights provide a fresh perspective on the topic. Looking forward to more engaging content from you.
    Vestibular Rehabilitation Therapy in Surrey

    ReplyDelete