【发布时间】:2015-04-18 11:52:19
【问题描述】:
我的问题与Get all hidden input fields in JSF dynamically 相关,但这与我想使用 JSF 而不是纯 HTML 不同,并假设我的 .xhtml 文件中有以下内容:
<h:inputHidden id="name1" value="SomeValue1"/>
<h:inputHidden id="name2" value="SomeValue2"/>
我开发了一个小代码,尝试动态获取所有h:inputHidden 标签并将它们的值打印到控制台,但问题是我无法弄清楚如何让所有东西都动态化。在我的代码中,如果我想迭代 uicomponents,我应该知道 id 的形式,如何迭代组件树中的所有 UIComponent? (我试过UIViewRoot#getChildren(),但我只得到了第一个孩子)。
这里是sn-p的代码:
// 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());
}
【问题讨论】:
标签: jsf jsf-2 uicomponents