【发布时间】:2020-06-26 11:33:13
【问题描述】:
我尝试将 java 列表放入 2D 数组并从 JSP 中检索它。但这没有成功。我真的需要这样做。如果可以的话,请支持我。如果你能解决这个问题,我已经把我的代码放在下面,我真的很感激。
更新:
第一个列表被插入到第一行(我希望它插入到第一列)。同样,其他三个列表插入到其他 3 行而不是 3 列。
屏幕截图
要求: 例如:-
Servlet 代码。
Scanner scanner = new Scanner(Result);
List<String> cdLine = new ArrayList<String>();
List<Integer> wtc = new ArrayList<Integer>();
List<Integer> ncc = new ArrayList<Integer>();
List<Integer> ccpps = new ArrayList<Integer>();
ControlData controlData = new ControlData();
while(scanner.hasNextLine())
{
token1 = scanner.nextLine();
Wtcs = controlData.CtrlWeight(token1);
NC = controlData.NofConditions(token1);
Ccspps = controlData.previousComplex(token1);
cdLine.add(token1);
wtc.add(Ccspps);
ncc.add(NC);
ccpps.add(Wtcs);
@SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
}
@SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
scanner.close(); //close the scanner
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/Control_structures.jsp");
request.setAttribute("Code_string", arr);
dispatcher.forward(request, response);
JSP 代码(使用 JSTL)
<c:forEach items="${Code_string}" var="post" varStatus="theCount">
<tbody>
<c:forEach items="${post}" var="value" varStatus="cell">
<tr>
<td scope="row">${theCount.count}</td>
<td>${value[0]}</td>
<td>${value[1]}</td>
<td>${value[2]}</td>
<td>${value[3]}</td>
<td>-</td>
</tr>
</c:forEach>
</tbody>
</c:forEach>
更新 2: 解析到 JSP 的二维数组是这样的。希望这也需要改变。
[[[public class Prime {, if, public static void main(String[] args) {, , int low = 20, high = 50;, , while (low System.out.print(low + " ");, , ++low;, }, }, , public static boolean checkPrimeNumber(int num) {,
布尔标志 = true;, , for(int i = 2; i if(num % i == 0) {, flag = false;,
break;, }, }, , 返回标志;, }, }]], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]]
【问题讨论】:
-
您遇到了哪些错误?你试过调试吗?
-
@Yserbius 它将所有 4 个列表分成四行。让我用屏幕截图更新问题
-
@Yserbius 你现在可以看看吗?
-
它应该显示什么?
-
@Yserbius 我已将 4 个列表添加到二维数组中。所以我想要的是我想将这 4 个列表显示为 4 列,但这里显示为 4 列。
标签: java jsp servlets foreach jstl