Get all hidden input fields in JSF dynamically

I have some hidden inputs that are generated dynamically using jQuery. For instance:

<input type="hidden" name="name1" value="SomeValue1"> <input type="hidden" name="name2" value="SomeValue2"> 

Method

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

returns the value SomeValue1 , which is correct. But at runtime, I don't know the input names. How can I get all hidden inputs without knowing their names?

Thansk for help.

+3
source share
1 answer

Give them the same name so that instead of getRequestParameterValuesMap()

 <input type="hidden" name="name" value="SomeValue1"> <input type="hidden" name="name" value="SomeValue2"> ... 
 String[] names = externalContext.getRequestParameterValuesMap().get("name"); 

It is guaranteed that the order will be the same as in the HTML DOM.

Alternatively, based on the suffix of the incremental integer, as in the HTML DOM, you can also just get the query 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; } } 
+4
source

Source: https://habr.com/ru/post/982636/


All Articles