My question is related to this one Get all hidden input fields in JSF dynamically but it\'s not the same as I want to work with JSF and not plain HTML, and assuming that I h
You also need to iterate over children of children, and their children, etcetera. You see, it's a component tree.
Here's a kickoff snippet of an utility method which does exactly that using tail recursion:
public static void findChildrenByType(UIComponent parent, List found, Class type) {
for (UIComponent child : parent.getChildren()) {
if (type.isAssignableFrom(child.getClass())) {
found.add(type.cast(child));
}
findChildrenByType(child, found, type);
}
}
Here's how you could use it:
UIForm form = (UIForm) FacesContext.getCurrentInstance().getViewRoot().findComponent("formId");
List hiddenComponents = new ArrayList<>();
findChildrenByType(form, hiddenComponents, HtmlInputHidden.class);
for (HtmlInputHidden hidden : hiddenComponents) {
System.out.println(hidden.getValue());
}
Or, better, use UIComponent#visitTree() which uses the visitor pattern. The major difference is that it also iterates over iterating components such as and and restores the child state for every iteration. Otherwise you would end up getting no value when you have an enclosed in such component.
FacesContext context = FacesContext.getCurrentInstance();
List
After all, it's probably easiest to just bind them to a bean property the usual way, if necessary inside an :