Retrieving other component's client ID in JSF 2.0

前端 未结 3 1079
[愿得一人]
[愿得一人] 2020-12-06 10:00

Does JSF 2.0 have a built-in method for finding the client ID of another component? There are about a thousand client ID-related questions on SO and there are a lot of hack

3条回答
  •  感情败类
    2020-12-06 10:32

    This worked for me. I would be interested to know if it is Ok to write response like this though.

    client.html

    
    

    UIHelper.java

    @ManagedBean(name = "UIHelper", eager = true)
    @ApplicationScoped
    public class UIHelper
    {
    
    public String clientId(final String id)
    {
      FacesContext context = FacesContext.getCurrentInstance();
      UIViewRoot root = context.getViewRoot();
      final UIComponent[] found = new UIComponent[1];
      root.visitTree(new FullVisitContext(context), new VisitCallback()
      {
        @Override
        public VisitResult visit(VisitContext context, UIComponent component)
        {
          if (component.getId().equals(id))
          {
            found[0] = component;
            return VisitResult.COMPLETE;
          }
          return VisitResult.ACCEPT;
        }
      });
      return found[0] == null ? "" : "#" + found[0].getClientId().replace(":", "\\\\:");
    }
    
    }
    

提交回复
热议问题