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

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

问题描述

如何获取 HTML 表单中的所有 parameterNames顺序一样吗?​​

How do I get all the parameterNames in an HTML form in the same sequence?

示例:

  • 如果表单包含FirstNameLastNameAge

输出应该完全相同的顺序

我尝试使用以下方法,但这改变了输出顺序:

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天全站免登陆