Pass 2D array in querystring to JSP

喜夏-厌秋 提交于 2019-12-13 04:18:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!