marți, 10 februarie 2015

[OmniFaces utilities (2.0)] Get a list of UI components matching the given type in children of the given component


[OmniFaces utilities] The findComponentsInChildren() returns a list of UI components matching the given type in children of the given component.

Method:
Usage:
JSF Page Snippet
<h:form id="myFormId">
 <h:inputText id="nameId" value="#{playersBean.name}"/>
 <h:inputText id="surnameId" value="#{playersBean.surname}"/>
 <h:inputText id="ageId" value="#{playersBean.age}"/>
 <h:selectOneMenu id="selectId" value="#{playersBean.title}">
  <f:selectItem id="rgItemId" itemValue="Roland Garros" itemLabel="Roland Garros" />
  <f:selectItem id="uoItemId" itemValue="US Open" itemLabel="US Open" />
  <f:selectItem id="aoItemId" itemValue="Australian Open" itemLabel="Australian Open" />
  <f:selectItem id="wItemId" itemValue="Wimbledon" itemLabel="Wimbledon" />
 </h:selectOneMenu>
 <h:commandButton id="btnId" value="Save" action="#{playersBean.save()}">
  <f:param id="auParamId" name="titles" value="Australian Open"/>
  <f:param id="rgParamId" name="titles" value="Roland Garros"/>
  <f:param id="wParamId" name="titles" value="Wimbledon"/>
  <f:param id="uoParamId" name="titles" value="US Open"/>              
 </h:commandButton>
</h:form>

Now, let's have a few examples to see how findComponentsInChildren() works (for all examples, we will use the UIForm (playersForm object) as the component to search in, but you can adjust this accordingly; the output contains the component class and the component clientId):

import org.omnifaces.util.Components;
...
·         find all components in form that are instances if UIInput (or derived from UIInput)

List<UIInput> inputs = Components.findComponentsInChildren(playersForm, UIInput.class);

Output:
 Class: class javax.faces.component.html.HtmlInputText    ClientId:myFormId:nameId
 Class: class javax.faces.component.html.HtmlInputText    ClientId:myFormId:surnameId
 Class: class javax.faces.component.html.HtmlInputText    ClientId:myFormId:ageId
 Class: class javax.faces.component.html.HtmlSelectOneMenu    ClientId:myFormId:selectId

·         find all components in form that are instances if UIParameter (or derived from UIParameter)

List<UIParameter> params = Components.findComponentsInChildren(playersForm, UIParameter.class);

Output:
Class: class javax.faces.component.UIParameter    ClientId:myFormId:auParamId
Class: class javax.faces.component.UIParameter    ClientId:myFormId:rgParamId
Class: class javax.faces.component.UIParameter    ClientId:myFormId:wParamId
Class: class javax.faces.component.UIParameter    ClientId:myFormId:uoParamId

·         find all components in form that are instances if UISelectItem (or derived from UISelectItem)

List<UISelectItem> selectitems = Components.findComponentsInChildren(playersForm, UISelectItem.class);

Output:
Class: class javax.faces.component.UISelectItem    ClientId:myFormId:rgItemId
Class: class javax.faces.component.UISelectItem    ClientId:myFormId:uoItemId
Class: class javax.faces.component.UISelectItem    ClientId:myFormId:aoItemId
Class: class javax.faces.component.UISelectItem    ClientId:myFormId:wItemId

·         find all components in form that are instances if UICommand (or derived from UICommand)

List<UICommand> commands = Components.findComponentsInChildren(playersForm, UICommand.class);

Output:

Class: class javax.faces.component.html.HtmlCommandButton    ClientId:myFormId:btnId

Niciun comentariu:

Trimiteți un comentariu