How to Pass JSF Parameters with HTTP GET Within the URL
In this recipe, you can see a quick method of retrieving parameters from JSF code.
Join the DZone community and get the full member experience.
Join For FreeIn this recipe, you can see a quick method of retrieving parameters from JSF code.
Getting ready
We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.
How to do it...
You can retrieve parameters using the #{param.parameter_name} expression, such as the following (notice that the parameter is named id, and we are using #{param.id} to retrieve its value):
<h:form id="formId"> <h:commandButton id="btn1Id" value="Pass parameter 100 ..." onclick="window.open('pagetwo.xhtml?id=100', 'MyWindow', 'height=350, width=250, menubar=no,toolbar=no'); return false;" /></h:form>...<h:outputText value="The parameter passed is: #{param.id}" />...
Another solution is to retrieve the value through a managed bean, as shown next:
<h:form id="formId"> <h:commandButton id="btn2Id" value="Pass parameter 200 ..." onclick="window.open('pagethree.xhtml?id=200', 'MyWindow', 'height=350, width=250,menubar=no,toolbar=no'); return false;" /></h:form>...<h:outputText value="The parameter passed is: #{bean.passedParameter}"/>...
The managed bean that actually retrieves the parameter value is:
package bean;import javax.faces.bean.ManagedBean;import javax.faces.bean.RequestScoped;import javax.faces.context.FacesContext;@ManagedBean@RequestScopedpublic class Bean { private String passedParameter; public String getPassedParameter() { FacesContext facesContext = FacesContext.getCurrentInstance(); this.passedParameter = (String) facesContext.getExternalContext(). getRequestParameterMap().get("id"); return this.passedParameter; } public void setPassedParameter(String passedParameter) { this.passedParameter = passedParameter; }}
How it works...
In the first example, the task is performed by the EL, #{param.parameter_name}, while, in the second example, the managed bean uses the getRequestParameterMap function, which has access to the GET request parameters.
You can find this recipe in JSF 2.0 Cookbook from Packt
From http://e-blog-java.blogspot.com/2011/03/how-to-pass-jsf-parameters-with-http.html
Opinions expressed by DZone contributors are their own.
Comments