My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...









[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


vineri, 20 martie 2015

[OmniFaces utilities (2.0)] Get the HTTP request parameter map / HTTP request parameter values map

[OmniFaces utilities] The getRequestParameterMap() method returns the HTTP request parameter map.
[OmniFaces utilities] The getRequestParameter() method returns the HTTP request parameter value associated with the given name.
[OmniFaces utilities] The getRequestParameterValuesMap() method returns the HTTP request parameter values map.
[OmniFaces utilities] The getRequestParameterValues() method returns the HTTP request parameter values associated with the given name.

Method Faces#getRequestParameterMap() - returns the HTTP request parameter map:
See also: Faces#getContext()

Method  Faces#getRequestParameter()returns the HTTP request parameter value associated with the given name:
See also: Faces#getContext()

Method Faces#getRequestParameterValuesMap() -  returns the HTTP request parameter values map:
See also: Faces#getContext()

Method Faces#getRequestParameterValues() - returns the HTTP request parameter values associated with the given name.
See also: Faces#getContext()

Let's suppose a simple form, as below (the typed e-mail is; the typed password is rafa2015):

<h:form id="loginFormId">                                        
 E-mail: <h:inputText id="emailId" required="true" value="#{}"/>
 Password: <h:inputSecret id="passwordId" required="true" value="#{loginBean.password}"/>                                          
 <h:commandButton id="submitId" value="Login" action="#{loginBean.loginAction()}">           
  <o:param name="name" value="Rafael"/>
  <o:param name="surname" value="Nadal"/>

The HTTP request parameter map can be accessed via Faces#getRequestParameterMap(), as below:

import org.omnifaces.util.Faces;
Map<String, String> requestParameterMap = Faces.getRequestParameterMap();
for (Map.Entry<String, String> entry : requestParameterMap.entrySet()) {
     System.out.println(entry.getKey() + "/" + entry.getValue());
     // do something with request parameters

For example, a possible output can be:


If you need only the value of a single parameter then you can use Faces#getRequestParameter(). For example, let's suppose that we are interested only in the loginFormId:passwordId parameter value. Then we write this:

import org.omnifaces.util.Faces;
// the 'passwordValue' will be 'rafa2015'
String passwordValue = Faces.getRequestParameter("loginFormId:passwordId");

If you need the "immutable Map whose keys are the set of request parameters names included in the current request, and whose values (of type String[]) are all of the values for each parameter name returned by the underlying request - Mojarra documentation", then you can use Faces#getRequestParameterValuesMap(), as below:

import org.omnifaces.util.Faces;
Map<String, String[]> requestParameterValuesMap = Faces.getRequestParameterValuesMap();
for (Map.Entry<String, String[]> entry : requestParameterValuesMap.entrySet()) {
     System.out.println(entry.getKey() + "/" + Arrays.toString(entry.getValue()));
     // do something with request parameters

For example, a possible output can be:


If you need to returns the HTTP request parameter values associated with the given name, then you can use Faces#getRequestParameterValues(). Let's try the same  loginFormId:passwordId parameter value (in this case, is a single value):

import org.omnifaces.util.Faces;
// the 'passwordValues' array will contain only 'rafa2015' value
String[] passwordValues = Faces.getRequestParameterValues("loginFormId:passwordId");

More details about request parameters in "JSF 2.2 - <h:form> and request/viewparameters".

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors