Java 怎么从指定的JSF组件开始查找具有指定ID的组件

2019年8月14日22:03:58Java 怎么从指定的JSF组件开始查找具有指定ID的组件已关闭评论 166 1350字阅读4分30秒

1.简介

Java 怎么从指定的JSF组件开始查找具有指定ID的组件?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.EvaluationException;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.Collection;

public class Main{
    
    static UIComponent findComponent(UIComponent findBase, String id,
            final char separatorChar) {
        if (!(findBase instanceof NamingContainer)
                && idsAreEqual(id, findBase)) {
            return findBase;
        }

        int facetCount = findBase.getFacetCount();
        if (facetCount > 0) {
            for (UIComponent facet : findBase.getFacets().values()) {
                if (!(facet instanceof NamingContainer)) {
                    UIComponent find = findComponent(facet, id,
                            separatorChar);
                    if (find != null) {
                        return find;
                    }
                } else if (idsAreEqual(id, facet)) {
                    return facet;
                }
            }
        }

        for (int i = 0, childCount = findBase.getChildCount(); i < childCount; i++) {
            UIComponent child = findBase.getChildren().get(i);
            if (!(child instanceof NamingContainer)) {
                UIComponent find = findComponent(child, id, separatorChar);
                if (find != null) {
                    return find;
                }
            } else if (idsAreEqual(id, child)) {
                return child;
            }
        }

        if (findBase instanceof NamingContainer
                && idsAreEqual(id, findBase)) {
            return findBase;
        }

        return null;
    }
    private static boolean idsAreEqual(String id, UIComponent cmp) {
        if (id.equals(cmp.getId())) {
            return true;
        }

        return false;
    }
}

 
继续阅读
编程十万个怎么办