MadNeox
MadNeox

Reputation: 2485

Get all hidden input fields in JSF dynamically

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">

The method

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("name1") 

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 ?

Thansk for help.

Upvotes: 2

Views: 1136

Answers (1)

BalusC
BalusC

Reputation: 1109062

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");

The ordering is guaranteed to be the same as in HTML DOM.

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;
    }
}

Upvotes: 3

Related Questions