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

Twitter

marți, 1 septembrie 2015

[OmniFaces utilities (2.2)] Convert the given parameter values list to request query string (ParamHolder)


[OmniFaces utilities] The toQueryString() method converts the given parameter values list to request query string. Empty names and null values will be skipped.

Method:
See also: Utils#encodeURL()
Usage:

For those who are familiar with OmniFaces, ParamHolder interface, and just want to see an example of Servlets#toQueryString() that uses ParamHolder, please skip the next paragraphs and follow the Example section below. But, if you have no idea what ParamHolder is, then you don't know what to pass instead of the List<ParamHolder> params argument. So, here it is a quick story of it provided by steps:

·         An UIComponent is aware of a converter if it implements the JSF built-in ValueHolder interface.
·         OmniFaces comes with an extension of ValueHolder, named ParamHolder. "This interface represents a (request) parameter holder which extends ValueHolder withgetName() method to obtain the parameter name associated with the parameter value and changes the getLocalValue() method to return the original, unconverted value and changes the getValue() method to return the converted value."
·         So, whenever you need such a (request) parameter, you can implement OmniFacesParamHolder interface, or simply extend the OmniFacesSimpleParam, which provides a simple and default implementation of it.
·         OmniFaces uses ParamHolder to boost the JSF default <f:param> to support a converter. See the OmniFaces<o:param> component, which implements ParamHolder.

Example:

Now, that you know what the List<ParamHolder> represents, let's see an example. I'm using the SimpleParam as a simple key-value pair holder (parameter name-value), no converter:

SimpleParam glo = new SimpleParam("glo_key", "glo_value");
SimpleParam fiz = new SimpleParam("fiz_key", "fiz_value");
SimpleParam foo = new SimpleParam("", "foo_value");
SimpleParam buzz = new SimpleParam("buzz_key", null);

List<ParamHolder> params = new ArrayList();

params.add(foo);
params.add(fiz);
params.add(glo);
params.add(buzz);

Further, we can pass the params to the Servlets#toQueryString():

import org.omnifaces.util.Servlets;
...
String queryString = Servlets.toQueryString(params);

Since empty names and null values are skipped, the resulted query string will be:

fiz_key=fiz_value&glo_key=glo_value

The above example uses the folowing SimpleParam constructor:


But, you can also use any of the following constructors (the last two are available since OmniFaces 2.2):



Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors