Java 怎么重置JSF输入组件

2019年8月14日22:10:35 评论 27

1.简介

Java 怎么重置JSF输入组件?——请看下文。

2.代码

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

 
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

public class Main{
    public static void main(String[] argv) throws Exception{
        String rootCompId = "tah1986.com";
        resetInputComponents(rootCompId);
    }
    public static void resetInputComponents(String rootCompId) {
        UIComponent rootUIComponent = findComponentById(rootCompId);
        resetInputComponents(rootUIComponent);
    }
    private static void resetInputComponents(UIComponent rootUIComponent) {
        if ((rootUIComponent == null)
                || (rootUIComponent.getChildCount() == 0)) {
            return;
        }

        for (UIComponent comp : rootUIComponent.getChildren()) {
            if (comp instanceof UIInput) {
                UIInput uiInput = (UIInput) comp;
                uiInput.setSubmittedValue(null);
                uiInput.setValid(true);
                uiInput.setLocalValueSet(false);
                uiInput.resetValue();
            }
            resetInputComponents(comp);
        }
    }
    public static UIComponent findComponentById(String compId) {
        return findComponentById(FacesContext.getCurrentInstance()
                .getViewRoot().getChildren(), compId);
    }
    private static UIComponent findComponentById(
            List<UIComponent> components, String compId) {
        if ((components == null) || (components.size() == 0)) {
            return null;
        }

        for (UIComponent comp : components) {
            if (compId.equals(comp.getId())) {
                return comp;
            }
            UIComponent tempResult = findComponentById(comp.getChildren(),
                    compId);
            if (tempResult != null) {
                return tempResult;
            }
        }

        return null;
    }
}

 

3.总结

示例代码演示了 Java 怎么重置JSF输入组件,具体步骤参见示例代码。

发表评论

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