Java 怎么查找指定类型的JSF组件

2019年8月14日22:07:59Java 怎么查找指定类型的JSF组件已关闭评论 211 1012字阅读3分22秒

1.简介

Java 怎么查找指定类型的JSF组件?——请看下文。

2.示例代码

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

import javax.faces.component.UIComponent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main{
    public static <S extends UIComponent> List<S> findComponentsOfType(
            UIComponent baseComponent, final Class<S> clazz) {
        final List<S> results = new ArrayList<>();
        recurseChildren(baseComponent, 0, new UIComponentVisitor() {
            @Override
            public boolean visit(UIComponent component, int depth) {
                if (clazz.isInstance(component)) {
                    results.add((S) component);
                }

                return true;
            }
        });

        return results;
    }
    
    public static boolean recurseChildren(UIComponent baseComponent,
            int depth, UIComponentVisitor visitor) {
        if (visitor.visit(baseComponent, depth) == false) {
            
            return false;
        }

        if (baseComponent.getFacetsAndChildren() != null) {

            Iterator<UIComponent> childComponentIterator = baseComponent
                    .getFacetsAndChildren();
            while (childComponentIterator.hasNext()) {
                UIComponent childComponent = childComponentIterator.next();
                if (recurseChildren(childComponent, depth + 1, visitor) == false) {
                    
                    return false;
                }
            }
        }

        
        return true;
    }
}

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