Passing parameters with HTTP GET within the URL

In the Passing parameters from JS to JSF (client to server) recipe, you saw how to pass parameters from client to server. One of the presented solutions passes parameters with HTTP GET within the URL. In this recipe, you can see a quick method of retrieving those 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
@RequestScoped
public 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.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Pass_parameters_with_HTTP_GET_within_the_URL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset