我的问题与此有关动态获取 JSF 中的所有隐藏输入字段,但这与我想使用 JSF 而不是纯 HTML 不同,并假设我的.xhtml
文件中有以下内容:
<h:inputHidden id="name1" value="SomeValue1"/>
<h:inputHidden id="name2" value="SomeValue2"/>
我开发了一个小代码,我试图h:inputHidden
动态获取所有标签并将它们的值打印到控制台,但问题是我无法弄清楚如何让一切都动态化。在我的代码中,id
如果我想遍历 uicomponents,我应该知道表单,如何遍历UIComponent
组件树中的所有内容?(我试过UIViewRoot#getChildren()
但我只得到第一个孩子)。
这是代码片段:
// 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());
}