将HTML表单数据数组发送到JSP / Servlet

时间:2021-07-06 11:49:00

I am coming from the PHP world, where any form data that has a name ending in square brackets automatically gets interpreted as an array. So for example:

我来自PHP世界,其中任何名称以方括号结尾的表单数据自动被解释为数组。例如:

<input type="text" name="car[0]" />
<input type="text" name="car[1]" />
<input type="text" name="car[3]" />

would be caught on the PHP side as an array of name "car" with 3 strings inside.

将在PHP端被捕获为名为“car”的数组,其中包含3个字符串。

Now, is there any way to duplicate that behavior when submitting to a JSP/Servlet backend at all? Any libraries that can do it for you?

现在,有没有办法在提交到JSP / Servlet后端时复制该行为?任何可以为你做的图书馆?

EDIT:

编辑:

To expand this problem a little further:

为了进一步扩展这个问题:

In PHP,

在PHP中,

<input type="text" name="car[0][name]" />
<input type="text" name="car[0][make]" />
<input type="text" name="car[1][name]" />

would get me a nested array. How can I reproduce this in JSP?

会得到一个嵌套数组。我怎样才能在JSP中重现这一点?

1 个解决方案

#1


10  

The [] notation in the request parameter name is a necessary hack in order to get PHP to recognize the request parameter as an array. This is unnecessary in other web languages like JSP/Servlet. Get rid of those brackets

请求参数名称中的[]表示法是必要的hack,以便让PHP将请求参数识别为数组。这在JSP / Servlet等其他Web语言中是不必要的。摆脱那些括号

<input type="text" name="car" />
<input type="text" name="car" />
<input type="text" name="car" />

This way they will be available by HttpServletRequest#getParameterValues().

这样,它们将由HttpServletRequest#getParameterValues()提供。

String[] cars = request.getParameterValues();
// ...

#1


10  

The [] notation in the request parameter name is a necessary hack in order to get PHP to recognize the request parameter as an array. This is unnecessary in other web languages like JSP/Servlet. Get rid of those brackets

请求参数名称中的[]表示法是必要的hack,以便让PHP将请求参数识别为数组。这在JSP / Servlet等其他Web语言中是不必要的。摆脱那些括号

<input type="text" name="car" />
<input type="text" name="car" />
<input type="text" name="car" />

This way they will be available by HttpServletRequest#getParameterValues().

这样,它们将由HttpServletRequest#getParameterValues()提供。

String[] cars = request.getParameterValues();
// ...