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 have the following in my .xhtml
file:
<h:inputHidden id="name1" value="SomeValue1"/>
<h:inputHidden id="name2" value="SomeValue2"/>
I developed a small code where I tried to get all the h:inputHidden
tags dynamically and print their values to the console, but the problem is that I can't figure out a way how to make everythning dynamic. In my code I should know the form id
if I want to Iterate over the uicomponents, how can I iterate over all the UIComponent
in the component tree ? (I tried UIViewRoot#getChildren()
but i get only the first childrens).
Here is the code snippet:
// formId is the id of my form
List<UIComponent> components = FacesContext.getCurrentInstance().getViewRoot().findComponent("formId").getChildren();
// A List of UIComponent where I am adding my Hidden Inputs
List<UIComponent> hiddenComponents = new ArrayList<UIComponent>();
for (UIComponent component : components) {
// using the hidden inputs type in JSF: HtmlInputHidden
if (component instanceof HtmlInputHidden) {
hiddenComponents.add(component);
}
}
for (UIComponent component : hiddenComponents) {
// Printing the hidden inputs values for demonstration purposes
System.out.println(((HtmlInputHidden)component).getValue());
}
findComponentsByType()
method on the UIViewRoot directly? in case i don't konw the form ID ? e.g:findComponentsByType(context.getViewRoot(), hiddenComponents, HtmlInputHidden.class);
is this correct ? also can we do the same withvisitTree
? – Alfred