Pages

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: JSFGet.zip

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

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/>: JSF12GET.zip

index.xhtml



Index.java


3 comments:

  1. is it just for Glassfish v3? Before this i hust GET data using f:meta. but when im trying your tutorial i got this message "Unable to create managed bean editController. The following problems were found: - The scope of the object referenced by expression #{param.view}, request, is shorter than the referring managed beans (editController) scope of view" with this URL:http://localhost:8080/Kuib_ContentSystem3/admin/post.jsf?view=1

    ReplyDelete
  2. This is not GlassFish specific. It is JSF 2.0 specific. I have included another example which was used on GlassFish 2.1.1 with Mojarra 2.1 installed.

    ReplyDelete
  3. TQ.. i found the problem is Session selected. That error happen when im using other than RequestSession...

    ReplyDelete