问题
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.
Example:
http://localhost:8080/queryWithQueryString?twodArray[0][0]=storeid&twodArray[0][1]=101
How can I achieve this?
回答1:
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]");
It's probably easier to use standard HTTP conventions for multiple parameter names.
http://localhost:8080/queryWithQueryString?twodArray[0]=storeid&twodArray[0]=101
with
String[][] twodArray = new String[1][];
twodArray[0] = request.getParameterValues("twodArray[0]");
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);
}
来源:https://stackoverflow.com/questions/13086441/pass-2d-array-in-querystring-to-jsp