Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Pass JSF Parameters with HTTP GET Within the URL

DZone's Guide to

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.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

In 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

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,frameworks

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}