sâmbătă, 14 martie 2015

[OmniFaces utilities (2.0/2.2)] Get the view parameters of the current view


[OmniFaces utilities] The getViewParameters() method returns the view parameters of the current view, or an empty collection if there is no view.
[OmniFaces utilities] The getViewParameterMap() method returns the view parameters of the current view as a parameter map, or an empty map if there is no view. This is ready for usage in among others ViewHandler#getBookmarkableURL(FacesContext, String, Map, boolean).

Method Faces#getViewParameters() - return view parameters as a Collection:

Method Faces#getViewParametersMap() - return view parameters as a Map (starting with OmniFaces 2.2, this method become mutable):
Usage:

Let's suppose that we have three view parameters, defined as:
...
<f:metadata>
 <f:viewParam name="email" value="#{loginBean.email}"/>
 <f:viewParam name="info" value="#{loginBean.info}"/>
 <f:viewParam name="type" value="guest"/>
</f:metadata>
...

Now, in a managed bean (or in other JSF artifact) you can "capture" the list of view parameters as below:

·         as a Collection:
import org.omnifaces.util.Faces;
...
Collection<UIViewParameter> vpsColl = Faces.getViewParameters();

·         as a Map:
import org.omnifaces.util.Faces;
...
Map<String, List<String>> vpsMap = Faces.getViewParameterMap();   

Note The <f:viewParam> doesn't support multiple values anyway, so having multiple <f:viewParam> on the same request parameter shouldn't end up in repeated parameters in action URL:

parameterMap.put(viewParameter.getName(), Collections.singletonList(value)); // OmniFaces 2.0
parameterMap.put(viewParameter.getName(), asList(value)); // OmniFaces 2.2

So, if you have a request to populate the view parameters, like:

http://host:port/app_name/?email=rafa@rg.com&info=Rafa%20wins%20RG%20again!

Then, Faces#getViewParameters() and Faces.getViewParameterMap() will reveal something like this (suppose that you have print to console the name and value of each view parameter):

Name: email Value: rafa@rg.com
Name: info  Value: Rafa wins RG again!
Name: type  Value: guest

Niciun comentariu:

Trimiteți un comentariu