0

I tried to do in a ManagedBean:

UIComponent a = FacesContext.getCurrentInstance().getViewRoot().findComponent("a:b");

I checked with the chrome browser inspector, and the component has id "a:b". But debugging the code, I get the UIComponent equal to null.

I tried with all the combinations: ":a:b", ":b", "b", "\\:a\\:b"...

I'm using Primefaces 3.4.1 with Mojarra 2.1.7

UPDATE: I also tried as suggested in the comments:

UIComponent container = FacesContext.getCurrentInstance().getViewRoot().findComponent("a");
UIComponent b = ((UIComponentBase) container).findComponent("b");

container is not null, but b is null

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
  • 1
    See https://docs.oracle.com/javaee/7/api/javax/faces/component/UIComponentBase.html#findComponent-java.lang.String- – Jasper de Vries Jul 13 '21 at 07:42
  • @JasperdeVries I updated the question. Anyway, can you explain why you removed Primefaces and jsf 2 tags? – Marco Sulla Jul 13 '21 at 08:25
  • 1
    The question does not relate to PrimeFaces (it's pure JSF functionality) and not to JSF 2.2 (you are using 2.1 and it can be applied to 2.3 as well, so no need to throw in these versions). – Jasper de Vries Jul 13 '21 at 09:29
  • @JasperdeVries Since I use Primefaces, maybe there's a solution that can be done with Primefaces. And since I use jsf 2, jsf 3 and 4 solutions are not valid. Furthermore people knows I have jsf 2 and not the old JSF 1. Can I put again these tags, please? – Marco Sulla Jul 13 '21 at 09:41
  • 1
    Please provide a [mcve]. Is `a` a naming container? What is your (relevant) xhtml structure? When and where do you call this code? – Jasper de Vries Jul 13 '21 at 09:58
  • @BalusC: I know you by fame, but I kindly disagree. I suspect the fact I can't find the element by id is a bug of `Primefaces` 3.4.1. So the tag `Primefaces`, at least, is needed. – Marco Sulla Jul 13 '21 at 10:11

1 Answers1

0

Solved inspired to Abbas Gadhia's answer:

public class JsfUtil {
   public static UIComponent findComponent(String id) {
       FacesContext context = FacesContext.getCurrentInstance();
       UIComponent root = context.getViewRoot();
       UIComponent res = root.findComponent(id);
       return res;
   }
   
   public static UIComponent findSubComponent(final String id, UIComponent container) {
       FacesContext context = FacesContext.getCurrentInstance();
       
       if (container == null) {
           container = context.getViewRoot();
       }
       
       String idContainer = container.getId();
       
       if (id.equals(idContainer)) {
            return container;
        }
       
       final UIComponent[] found = new UIComponent[1];
       VisitContext visitContext = VisitContext.createVisitContext(context);
       
       container.visitTree(visitContext, new VisitCallback() {
           @Override
           public VisitResult visit(
               VisitContext context, 
               UIComponent component
           ) {
               String idComponent = component.getId();
               
               if (id.equals(idComponent)) {
                   found[0] = component;
                   return VisitResult.COMPLETE;
               }
               
               return VisitResult.ACCEPT;
           }
       });
       
       return found[0];
   }
}

Usage:

UIComponent container = JsfUtil.findComponent("containerId");
UIComponent element = JsfUtil.findSubComponent("lastPartOfElementId", container);
Marco Sulla
  • 15,299
  • 14
  • 65
  • 100