如何以 PHP 和 Rails 的风格在 Java 中获取数组的请求参数?

发布于 2024-07-20 06:23:49 字数 249 浏览 6 评论 0原文

情况如下:

page.jsp?var[0]=foo&var[1]=bar

在Java中如何在数组中检索this?

以下内容:

page.jsp?var=foo&var=bar

我知道可以使用 request.getParameterValues("var") 检索

上述内容,但有任何解决方案吗?

The situation is as follows:

page.jsp?var[0]=foo&var[1]=bar

How can this be retrieved in an array in Java?

The following:

page.jsp?var=foo&var=bar

I know can be retrieved using request.getParameterValues("var")

Any solutions for the above though?

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

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

发布评论

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

评论(2

逆流 2024-07-27 06:23:49
Map<Integer,String> index2value=new HashMap<Integer,String>();

for (Enumeration e = request.getParameterNames(); e.hasMoreElements() ;)
 {
 String param= e.nextElement().toString();
 if(!param.matches("var\[[0-9]+\]")) continue;
 int index= (here extract the numerical value....)
 index2value.put(index,request.getParameter(param));
 }

希望这可以帮助。

Map<Integer,String> index2value=new HashMap<Integer,String>();

for (Enumeration e = request.getParameterNames(); e.hasMoreElements() ;)
 {
 String param= e.nextElement().toString();
 if(!param.matches("var\[[0-9]+\]")) continue;
 int index= (here extract the numerical value....)
 index2value.put(index,request.getParameter(param));
 }

Hope this helps.

夜雨飘雪 2024-07-27 06:23:49
HashMap m = request.getParameterMap();
Set k = m.keySet();
Set v = m.entrySet();
Object o[] = m.entrySet().toArray();

这将为您提供一个带有 K、V 对以及一组键和一组值的 Map 调用 m。 您几乎可以像数组一样迭代这些集合。 您还可以使用 toArray 将其转换为数组。

HashMap m = request.getParameterMap();
Set k = m.keySet();
Set v = m.entrySet();
Object o[] = m.entrySet().toArray();

That will get you a Map call m with K,V pairs and both a set of keys and set of values. You can iterate those sets almost like an array. You can also use toArray to turn it into an array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文