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.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):
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()
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
// 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())
// 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.
Niciun comentariu :
Trimiteți un comentariu