英文:
How foreach works for List of Lists in JSP
问题
> ["A","B","C","D","E","F"] - ArrayList 01
>
> [1,2,3,4,5,6] - ArrayList 02
>
> [10,20,30,40,50,60] - ArrayList 03
>
> [100,200,300,400,500,600] - ArrayList 04
>
> Main_List -
> [["A","B","C","D","E","F"],[1,2,3,4,5,6],[10,20,30,40,50,60],[100,200,300,400,500,600]];
- Main_List是由四个Array列表组合而成
如何将Main_List呈现为一个具有四列的表格?
<c:forEach items="${List_parameter}" var="post" varStatus="theCount">
<tbody>
<c:forEach items="${post}" var="value" varStatus="cell">
<tr>
<td scope="row">${theCount.count}</td>
<td>${value.get(0)}</td>
<td>${value.get(1)}</td>
<td>${value.get(2)}</td>
<td>${value.get(3)}</td>
<td>-</td>
</tr>
</c:forEach>
</tbody>
</c:forEach>
**Servlet将列表传递给JSP页面**
Scanner scanner = new Scanner(Result);
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);
ccp.add(Wtcs);
}
List arr[]={cdLine,wtc,ncc,ccp};
scanner.close(); //关闭扫描器
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/Control_structures.jsp");
request.setAttribute("Code_string", arr);
dispatcher.forward(request, response);
注意:我已经根据您的要求进行了翻译,只提取了代码部分并进行了翻译,没有包含其他内容。
英文:
> ["A","B","C","D","E","F"] - ArrayList 01
>
> [1,2,3,4,5,6] - ArrayList 02
>
> [10,20,30,40,50,60] - ArrayList 03
>
> [100,200,300,400,500,600] - ArrayList 04
>
> Main_List -
> [["A","B","C","D","E","F"],[1,2,3,4,5,6],[10,20,30,40,50,60],[100,200,300,400,500,600]];
- Main_List is combination of four Array-lists
How can I retreive Main_List in to a table which has four columns?
<c:forEach items="${List_parameter}" var="post" varStatus="theCount">
<tbody>
<c:forEach items="${post}" var="value" varStatus="cell">
<tr>
<td scope="row">${theCount.count}</td>
<td>${value.get(0)}</td>
<td>${value.get(1)}</td>
<td>${value.get(2)}</td>
<td>${value.get(3)}</td>
<td>-</td>
</tr>
</c:forEach>
</tbody>
</c:forEach>
Servlet passing the list to JSP
Scanner scanner = new Scanner(Result);
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);
}
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);
}
答案1
得分: 1
我得到了我问题的答案。
**在servlet中**
Scanner scanner = new Scanner(Result);
ControlData controlData = new ControlData();
List<List<Comparable>> p = new ArrayList<List<Comparable>>();
while (scanner.hasNextLine()) {
token1 = scanner.nextLine();
Wtcs = controlData.CtrlWeight(token1);
NC = controlData.NofConditions(token1);
Ccspps = controlData.previousComplex(token1);
List<Comparable> c = new ArrayList<Comparable>();
c.add(token1);
c.add(Wtcs);
c.add(NC);
c.add(Ccspps);
p.add(c);
}
scanner.close(); //关闭扫描仪
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/jsp_page.jsp");
request.setAttribute("Code_string", p);
dispatcher.forward(request, response);
现在我有一个简单的列表要传递给JSP。
<c:forEach items="${Code_string}" var="post" varStatus="theCount1">
<tbody>
<tr>
<td>${post[0]}</td>
<td>${post[1]}</td>
<td>${post[2]}</td>
<td>${post[3]}</td>
</tr>
</tbody>
</c:forEach>
英文:
I got the answer for my question.
In servlet
Scanner scanner = new Scanner(Result);
ControlData controlData = new ControlData();
List<List<Comparable>> p =new ArrayList<List<Comparable>>();
while(scanner.hasNextLine())
{
token1 = scanner.nextLine();
Wtcs = controlData.CtrlWeight(token1);
NC = controlData.NofConditions(token1);
Ccspps = controlData.previousComplex(token1);
List<Comparable> c =new ArrayList<Comparable>();
c.add(token1);
c.add(Wtcs);
c.add(NC);
c.add(Ccspps);
p.add(c);
}
scanner.close(); //close the scanner
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/jsp_page.jsp");
request.setAttribute("Code_string", p);
dispatcher.forward(request, response);
Now I have a simple list to pass the JSP.
<c:forEach items="${Code_string}" var="post" varStatus="theCount1">
<tbody>
<tr>
<td>${post[0]}</td>
<td>${post[1]}</td>
<td>${post[2]}</td>
<td>${post[3]}</td>
</tr>
</tbody>
</c:forEach>
答案2
得分: -1
Sure, here is the translated code you provided:
<!DOCTYPE html>
<%@ page isELIgnored="false"%>
<%@ page language="java" contentType="text/html"%>
<%@ page import="java.util.*"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List</title>
</head>
<body>
<h1>List</h1>
<%
List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
List<List> finalList = new ArrayList<>();
finalList.add(list1);
finalList.add(list2);
pageContext.setAttribute("list", finalList);
%>
<c:forEach items="${list}" var="post" varStatus="theCount">
<p>List ${theCount.count}</p>
<c:forEach items="${post}" var="value" varStatus="cell">
${value}
</c:forEach>
</c:forEach>
</body>
</html>
//Output//
List
List 1
A B C D E F G
List 2
1 2 3 4 5 6 7
Please note that the translated code retains the original structure and content while providing the translations for the non-code elements.
<details>
<summary>英文:</summary>
1) Use <%@ page isELIgnored="false"%> evaluate EL
2) Create List<List> and add.
3) Set pageContext.setAttribute("list", finalList);
<!DOCTYPE html>
<%@ page isELIgnored="false"%>
<%@ page language="java" contentType="text/html"%>
<%@ page import="java.util.*"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List</title>
</head>
<body>
<h1>List</h1>
<%
List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
List<List> finalList = new ArrayList<>();
finalList.add(list1);
finalList.add(list2);
pageContext.setAttribute("list", finalList);
%>
<c:forEach items="${list}" var="post" varStatus="theCount">
<p>List ${theCount.count}</p>
<c:forEach items="${post}" var="value" varStatus="cell">
${value} &nbsp;
</c:forEach>
</c:forEach>
</body>
</html>
//Output//
List
List 1
A B C D E F G
List 2
1 2 3 4 5 6 7
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论