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

duminică, 28 iunie 2015

[OmniFaces utilities 2.2] Unmodifiable list with all child UIParameter components of the given parent component


THIS METHOD IS AVAILABLE IN 2.1, BUT IT IS RECOMMENDED TO USE OmniFaces 2.2 SNAPSHOT (FINAL) or higher!

Starting with version 2.2 - SNAPSHOT (FINAL) the
 Components#getParams() was fixed to take into account converter of ParamHolder.

[OmniFaces utilities] The getParams() method returns an unmodifiable list with all child UIParameter components (<f|o:param>) of the given parent component as a list of ParamHolder instances. Those with disabled=true and an empty name are skipped.

Method (left side uses org.omnifaces.component.SimpleParam from version 2.1, while right side uses SimpleParam from version 2.2):

Usage:

Before seeing an example of using the Components#getParams(), is important to inspect the above source code and to notice that it relies on org.omnifaces.component.ParamHolder and org.omnifaces.component.SimpleParam. The ParamHolder is a simple interface that extends standard ValueHolder in order to provide:

-          a method for obtaining the parameter name associated with the parameter value
-          a method to return the original, unconverted value of the parameter
-          a method to return the converted value of the parameter (especially needed for <o:param>)

package org.omnifaces.component;

import javax.faces.application.Application;
import javax.faces.component.ValueHolder;
import javax.faces.convert.Converter;

import org.omnifaces.util.Faces;

public interface ParamHolder extends ValueHolder {

 // returns the name of the parameter
 public String getName();

 // returns the original, unconverted value of the parameter
 @Override
 public Object getLocalValue();

 // returns the converted value of the parameter
 @Override
 public Object getValue();
}

The SimpleParam provides a basic and default implementation of the ParamHolder interface. Ultimately, this class can be used as a simple key-value pair holder (parameter name-value) which uses an explicit/implicit JSF converter to convert the object value to string.

Now, let's suppose that we have the following simple form (notice that, for variety, the #{param['age']} is just a request parameters passed in the URL query string as ?age=24):

<h:form id="form">
 <h:commandButton id="submit" value="Send" action="#{myBean.sendAction()}">
  <f:param name="age" value="#{param['age']}"/>
  <f:param name="name" value="#{myBean.name}"/>
  <o:param name="address" value="#{myBean.address}" converter="addressConverter"/>
 </h:commandButton>
</h:form>

Further, we can use the Components#getParams() utility to programmatically collect the UIParameters  (<f|o:param>) nested in the above <h:commandButton/>. This can be accomplish in different JSF artifacts; for example, in a bean (obviously, there are several ways to identify the parent UIComponent of the UIParameters that we want to collect - the UIComponent that should be passed to the #getParams() should be obtained in a convenient manner depending on your circumstances; below, we simply used the Components#findComponent() utility):

UIComponent uc = Components.findComponent("form:submit"); // see: Components#findComponent()
List<ParamHolder> params = Components.getParams(uc);

for (ParamHolder ph : params) {
    // the getName() will return the parameter name

    // starting with version 2.2 
    // getLocalValue() returns the unconverted value
       // getValue() returns the converted value (passed through getAsString())    
}

Note Don't worry about <o:param> with attached converter, OmniFaces 2.2 takes care of it. 

API 2.1 GH 2.1

By Anghel Leonard and Constantin Alin

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors