request.getParameterNames() 的顺序

发布于 2024-10-12 21:42:28 字数 475 浏览 3 评论 0原文

如何获取 HTML 表单中的所有 parameterNames 相同的顺序?

示例:

  • 如果表单包含FirstNameLastNameAge

  • 输出应在 中显示 exatcly相同的序列

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

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

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

Example:

  • If the form contains FirstName, LastNameand Age

  • The output should appear exatcly 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

纵性 2024-10-19 21:42:28

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

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

之后您基本上可以按前缀对元素进行排序。这是一个丑陋的解决方案,但这是唯一的方法。比如:

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

顺便说一句 - 您接收参数的顺序真的很重要吗?

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 ?

-残月青衣踏尘吟 2024-10-19 21:42:28

这里的答案都没有真正回答我的问题。 HttpServletRequest 将其所有参数保存在 HashMap 中,并且 HashMap 没有顺序。因此,我将参数的顺序保存在有序的ArrayList中,并将其保存在HttpSession中,这样我就可以通过查询ArrayList(保存在会话中)来检索参数的顺序并实现我想要的!

None of the answers here really did answer my question. A HttpServletRequest saves all it's parameters in a HashMap, and a HashMap has NO ORDER. So, I saved the order of the parameters in an ordered ArrayList and saved it in a HttpSession, so I could retrieve the order of the parameters by querying the ArrayList (that was saved in the session) and achieve what I wanted!

要走干脆点 2024-10-19 21:42:28

request.getParameterNames()内部使用HashMap来存储表单字段的名称值对。这里没有维持秩序。如果您需要按顺序排列,表单参数的某种命名约定来控制检索顺序。

SortedSet temp = new SortedSet();
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) 
{
        temp.add((String)enumeration.nextElement());
}

request.getParameterNames () uses HashMap internally to store the name value pairs of form fields. There is no order maintained in this. if you need this in order then , some sort of naming convention for form parameters to control the order of retrieval.

SortedSet temp = new SortedSet();
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) 
{
        temp.add((String)enumeration.nextElement());
}
水波映月 2024-10-19 21:42:28

更新:您可以为此使用排序集。请注意,您必须拥有具有不同名称的所有参数(在这种情况下这是最有可能的)。写入任何前缀作为参数名称。

示例:

<input type="text" name="1step">
<input type="text" name="2step">

那么在java代码中你可以这样写:

SortedSet ss = new TreeSet();
Enumeration<String> enm = request.getParameterNames();
while(enm.hasMoreElements()) {
    String pname = enm.nextElement();
}
Iterator i = ss.iterator();
while(i.hasNext()) {
    String param = (String)i.next();
    String value = request.getParameter(param);
}
    

Updated: You can use sorted set for that. Note that you must have all the parameters with different names (in this case it is most likely). Write any prefix as your parameter name.

Example:

<input type="text" name="1step">
<input type="text" name="2step">

Then in java code you can write:

SortedSet ss = new TreeSet();
Enumeration<String> enm = request.getParameterNames();
while(enm.hasMoreElements()) {
    String pname = enm.nextElement();
}
Iterator i = ss.iterator();
while(i.hasNext()) {
    String param = (String)i.next();
    String value = request.getParameter(param);
}
    
江心雾 2024-10-19 21:42:28

HTML 或 Jsp 页面

<input type="text" name="1UserName">
<input type="text" name="2Password">
<input type="text" name="3MobileNo">
<input type="text" name="4country">

等等...

然后在 java 代码中

SortedSet ss = new TreeSet();
Enumeration<String> enm=request.getParameterNames();
while(enm.hasMoreElements()){
    String pname = enm.nextElement();
    ss.add(pname);
}
Iterator i=ss.iterator();
while(i.hasNext()) {
    String param=(String)i.next();
    String value=request.getParameter(param);
}

HTML or Jsp Page

<input type="text" name="1UserName">
<input type="text" name="2Password">
<input type="text" name="3MobileNo">
<input type="text" name="4country">

and so on...

then in java code

SortedSet ss = new TreeSet();
Enumeration<String> enm=request.getParameterNames();
while(enm.hasMoreElements()){
    String pname = enm.nextElement();
    ss.add(pname);
}
Iterator i=ss.iterator();
while(i.hasNext()) {
    String param=(String)i.next();
    String value=request.getParameter(param);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文