request.getParameterNames() 的顺序
如何获取 HTML 表单中的所有 parameterNames
相同的顺序?
示例:
如果表单包含
FirstName
、LastName
和Age
输出应在 中显示 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
,LastName
andAge
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为 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:
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:
By the way - does it really matters the order in which you receive the parameters ?
这里的答案都没有真正回答我的问题。 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!
request.getParameterNames()
内部使用HashMap
来存储表单字段的名称值对。这里没有维持秩序。如果您需要按顺序排列,表单参数的某种命名约定来控制检索顺序。request.getParameterNames ()
usesHashMap
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.更新:您可以为此使用排序集。请注意,您必须拥有具有不同名称的所有参数(在这种情况下这是最有可能的)。写入任何前缀作为参数名称。
示例:
那么在java代码中你可以这样写:
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:
Then in java code you can write:
HTML 或 Jsp 页面
等等...
然后在 java 代码中
HTML or Jsp Page
and so on...
then in java code