request.getParameterNames()的顺序 [英] Order of request.getParameterNames()

查看:458
本文介绍了request.getParameterNames()的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在
中的HTML表单中获取与表单中相同的序列中的所有parameterNames。

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

即表单是否包含。 ... FirstName,LastName,
MiddleName和Age。输出应出现在
相同的序列中

i.e if the form contains .... FirstName, LastName, MiddleName and Age . the output should appear in the same sequence

我尝试使用以下内容但这会改变输出的
顺序:

I have tried using the following but this shifts the order of the output:

Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements())
{
      String paramName =
(String)paramNames.nextElement();
      out.print(paramName);
}


推荐答案

我认为没有HTTP规范中没有任何内容强制浏览器按照它们在表单中出现的顺序发送参数。您可以通过在参数名称前加上一个数字来处理它,例如:

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<String>() {
       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 ?

这篇关于request.getParameterNames()的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆