将查询字符串中的2D数组传递给JSP [英] Pass 2D array in querystring to JSP
问题描述
我想在查询字符串中创建一个2D数组并将其传递给JSP.我可以追加字符串,但是找不到追加二维数组的语法.
I would like to create a 2D array in the query string and pass it to JSP. I could append strings, but I couldn't find the syntax to append two dimensional arrays.
示例:
http://localhost:8080/queryWithQueryString?twodArray [0] [0] = storeid& twodArray [0] [1] = 101
http://localhost:8080/queryWithQueryString?twodArray[0][0]=storeid&twodArray[0][1]=101
我该如何实现?
推荐答案
您可以按原样使用它.参数名称将以确切的格式到达字面上. JSP对此没有任何特殊处理(与PHP不同).因此,您需要解析您自己.
You can just use that as-is. The parameter name will arrive literally in exact that form. JSP doesn't have any special treatment for this (unlike for example PHP). So you'd need to parse it yourself.
String[][] twodArray = new String[1][];
twodArray[0] = new String[2];
twodArray[0][0] = request.getParameter("twodArray[0][0]");
twodArray[0][1] = request.getParameter("twodArray[0][1]");
对多个参数名称使用标准的HTTP约定可能会更容易.
It's probably easier to use standard HTTP conventions for multiple parameter names.
http://localhost:8080/queryWithQueryString?twodArray [0] = storeid& twodArray [0] = 101
http://localhost:8080/queryWithQueryString?twodArray[0]=storeid&twodArray[0]=101
使用
String[][] twodArray = new String[1][];
twodArray[0] = request.getParameterValues("twodArray[0]");
使用List<String[]>
代替String[][]
也可能更容易,因为List
可以动态扩展.如果您事先不知道商品数量,这将很有帮助.
It's probably also easier to use a List<String[]>
instead of String[][]
as a List
can be dynamically expanded. This is helpful if you don't know the amount of items beforehand.
List<String[]> twodArray = new ArrayList<String[]>();
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String[] values = request.getParameterValues("twodArray[" + i + "]");
if (values == null) break;
twodArray.add(values);
}
这篇关于将查询字符串中的2D数组传递给JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!