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

marți, 27 ianuarie 2015

JSF Method to Recursively Find Component in Component Tree

Whenever you want to find a component in the component tree, you can use:
But, sometimes you need to get down to the most time consuming search. Use, this approach as the last solution (pass to it the component tree/sub-tree in which to search and the clientId of the component to find - if the searched component cannot be found, this method returns null):

import static org.omnifaces.util.Utils.isEmpty;
...
private static <C extends UIComponent> C findComponentRecursively(UIComponent component, String clientId) {

 if (isEmpty(clientId)) {
     return null;
 }

 for (UIComponent child : component.getChildren()) {
      UIComponent result;

      try {
          result = child.findComponent(clientId);
      } catch (IllegalArgumentException e) {
        continue;
      }

      if (result == null) {
          result = findComponentRecursively(child, clientId);
     }

     if (result != null) {
         return (C) result;
     }
 }
 return null;

}

Examples:
JSF page snippet
...
<h:body> 
 <h:graphicImage id="myImageId" url="http://1.bp.blogspot.com/-
  gtjrtr50mMY/VL0IcD0hbrI/AAAAAAAAA-I/8fmOugRBkvU/s1600/omniutil.png"/>
 <h:form id="myFormId">
  <h:panelGrid id="panelId">             
   Some text
   <h:outputText id="myOutputId" value="..." />
   <h:panelGroup id="myDivId" layout="block">
    <h:inputText id="myInputId" value="..."/>                                     
   </h:panelGroup>
   <h:commandButton id="myButtonId" value="Click me!" />           
  </h:panelGrid>
 </h:form>
</h:body>
...

·         search from UIViewRoot the component with clientId myImageId
UIComponent found = findComponentRecursively(UIViewRoot, "myImageId");

·         search from UIViewRoot the component with id, myInputId / clientId, myFormId:myInputId

UIComponent found = findComponentRecursively(UIViewRoot, "myInputId");

//this will perform faster
UIComponent found = findComponentRecursively(UIViewRoot, "myFormId:myInputId");


Note  Whenever you can, is better to use clientIds instead of ids, because the search perform faster.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors