动态获取JSF中所有隐藏的输入字段 [英] Get all hidden input fields in JSF dynamically
问题描述
我有一些使用JQuery动态生成的隐藏输入.例如:
I have some hidden inputs that are generated dynamically using JQuery. For example :
<input type="hidden" name="name1" value="SomeValue1">
<input type="hidden" name="name2" value="SomeValue2">
方法
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("name1")
返回正确的值SomeValue1
.但是,在运行时,我不知道输入名称.如何在不知道其名称的情况下获取所有隐藏的输入?
returns the value SomeValue1
which is correct. But, at runtime I am not aware of the input names. How can I get all the hidden inputs without knowing their names ?
感谢您的帮助.
推荐答案
Give them all the same name, so that you can use getRequestParameterValuesMap()
instead.
<input type="hidden" name="name" value="SomeValue1">
<input type="hidden" name="name" value="SomeValue2">
...
String[] names = externalContext.getRequestParameterValuesMap().get("name");
保证顺序与HTML DOM中的顺序相同.
The ordering is guaranteed to be the same as in HTML DOM.
或者,基于HTML DOM中的增量整数后缀,您也可以循环获取request参数,直到返回null
.
Alternatively, based on the incremental integer suffix as you've in the HTML DOM, you could also just get the request parameter in a loop until null
is returned.
List<String> names = new ArrayList<>();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
String name = requestParameterMap.get("name" + i);
if (name != null) {
names.add(name);
} else {
break;
}
}
这篇关于动态获取JSF中所有隐藏的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!