reset JSF Input Components - Java JSF

Java examples for JSF:UIComponent

Description

reset JSF Input Components

Demo Code

/*/*w w  w  .j av a  2  s . co m*/
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */
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 = "java2s.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;
    }
}

Related Tutorials