![]() |
JSF 2.0 GET request |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <? xml version = '1.0' encoding = 'UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < h:head > < title >Passing GET parameters to JSF using @ManagedProperty</ title > </ h:head > < h:body > < h1 >Passing GET parameters to JSF using @ManagedProperty</ h1 > < p >To see the parameters use the link, or form below:</ p > < pre > < a href = "http://localhost:8080/JSFGet/index.jsf?x=100&y=1000" >http://localhost:8080/JSFGet/index.jsf?x=100&y=1000</ a > </ pre > < form action = "index.jsf" method = "GET" > < table border = "0" > < tbody > < tr > < td >x</ td > < td > < input type = "text" name = "x" value = "" /></ td > </ tr > < tr > < td >y</ td > < td >< input type = "text" name = "y" value = "" /></ td > </ tr > < tr > < td ></ td > < td >< input type = "submit" value = "Submit" /></ td > </ tr > </ tbody > </ table > </ form > < h:panelGrid rendered = "#{index.x ne 0 or index.y ne 0}" > < ul > < li >#{index.x}</ li > < li >#{index.y}</ li > </ ul > </ h:panelGrid > </ h:body > </ html > |
Index.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /* * Copyright 2011 John Yeary <jyeary@bluelotussoftware.com>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.bluelotussoftware.example.jsf; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.RequestScoped; /** * * @author John Yeary <jyeary@bluelotussoftware.com> * @version 1.0 */ @ManagedBean @RequestScoped public class Index { @ManagedProperty (value = "#{param.x}" ) private int x; @ManagedProperty (value = "#{param.y}" ) private int y; public Index() { } public int getX() { return x; } public void setX( int x) { this .x = x; } public int getY() { return y; } public void setY( int y) { this .y = y; } } |
3 comments :
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
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.
TQ.. i found the problem is Session selected. That error happen when im using other than RequestSession...
Post a Comment