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

2019年8月14日22:03:58 评论 28

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;
    }
}

 

3.总结

示例代码演示了 Java 怎么从指定的JSF组件开始查找具有指定ID的组件,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: