Wednesday, August 24, 2011

JSF Trick of the Day: @ManagedProperty set using GET request

JSF 2.0 GET request
This tip is designed to show how you can use plain HTML form, or parametrized strings to pass data to a JSF page, or backing bean.

This provides some additional flexibility when working with mixed environments. JSF 2.0 supports GET requests, but this is a simple alternative for mixed environments.

Jacob Hookam published an article called JSF 1.2 RI - Bean Instantiation and Annotations which mentions the use of <managed-property> to do this with JSF 1.2 in 2007.

This is a complete example using JSF 2.0 and annotations.

We use the @ManagedProperty annotation which replaces the faces-config.xml equivalent. We set the value of the managed property using the param implicit request object. This allows very simple substitutions for values.

In this example, we use a Facelets file which consists mostly of HTML with some JSF markup. The file contains a plain HTML form which is posting back to the same page with parameters which the user can enter. Alternatively, there is a hyperlink which accomplishes the same thing. Once the values are entered, or the hyperlink is clicked, the values will appear on the page.

The source code for the NetBeans 7.0.1 project can be found here:

Here is an alternate version which was confirmed on GlassFish 2.1.1:  

JSF 1.2 Example: This example was done using JSF 1.2 for those who may need it. It uses a faces-config.xml to configure the <managed-property/>:



Popular Posts