Order of request.getParameterNames()

后端 未结 5 1036
无人共我
无人共我 2021-01-04 04:36

How do I get all the parameterNames in an HTML form in the same sequence as they are in the form.

i.e if the form contains .... FirstName, LastName, MiddleName and

5条回答
  •  被撕碎了的回忆
    2021-01-04 04:53

    I don't think there's nothing in the HTTP spec that forces browsers to send parameters in the order they appear in the form. You can work it around by prefixing a number to the name of the parameter like:

    FirstName --> 0_FirstName
    LastName --> 1_LastName
    ....
    

    After that you could basically order the elements by the prefix. It is an ugly solution but it is the only way to do it. Something like ...

    //Assuming you fill listOfParameters with all the parameters.
        Collections.sort(listOfParameters, new Comparator() {
           int compare(String a,String b) {
                return Integer.getInt(a.substring(0,a.indexOf("_"))) - Integer.getInt(a.substring(0,b.indexOf("_")))
           }
        }
        );
        for (String param : listOfParameters) {
            //traverse in order of the prefix
        }
    

    By the way - does it really matters the order in which you receive the parameters ?

提交回复
热议问题