[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 OmniFaces, ParamHolder interface, or simply extend the OmniFaces, SimpleParam, 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.
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:
Niciun comentariu :
Trimiteți un comentariu