Find component by ID in JSF

前端 未结 5 1265
梦如初夏
梦如初夏 2020-11-29 01:48

I want to find some UIComponent from managed bean by the id that I have provided.

I have written the following code:

private UIComponen         


        
5条回答
  •  甜味超标
    2020-11-29 01:53

    Yes, in all parent components which are NamingContainers you have to add attribute prependId="false" - it will works in for sure and should work in others.
    If it is not possible to set it via attribute in .xhtml file you have to set such value programaticaly.

    Suggestion after question's author comment:

    If there is not such attribute in components that you are using try write find method like this:

    private UIComponent findComponent(String id, UIComponent where) {
    if (where == null) {
       return null;
    }
    else if (where.getId().equals(id)) {
       return where;
    }
    else {
       List childrenList = where.getChildren();
       if (childrenList == null || childrenList.isEmpty()) {
          return null;
       }
       for (UIComponent child : childrenList) {
          UIComponent result = null;
          result = findComponent(id, child);
          if(result != null) {
             return result;
       }
       return null;
    }   
    

    Next just invoke

    UIComponent iamLookingFor = findComponent(myId, FacesContext.getCurrentInstance().getViewRoot());
    

    That will help?

提交回复
热议问题