如何在JSP中对列表列表使用foreach工作

huangapple go评论104阅读模式
英文:

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&lt;List&lt;Comparable&gt;&gt; p =new ArrayList&lt;List&lt;Comparable&gt;&gt;();
		while(scanner.hasNextLine())  
		{  
			token1 = scanner.nextLine();
			Wtcs = controlData.CtrlWeight(token1);
			NC = controlData.NofConditions(token1);
			Ccspps = controlData.previousComplex(token1);
			List&lt;Comparable&gt; c =new ArrayList&lt;Comparable&gt;();
			c.add(token1);
			c.add(Wtcs);
			c.add(NC);
			c.add(Ccspps);
			p.add(c);
		}  
		scanner.close();     //close the scanner  
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(&quot;/views/jsp_page.jsp&quot;);
		request.setAttribute(&quot;Code_string&quot;, p);
		dispatcher.forward(request, response);

Now I have a simple list to pass the JSP.

&lt;c:forEach items=&quot;${Code_string}&quot; var=&quot;post&quot; varStatus=&quot;theCount1&quot;&gt;
		&lt;tbody&gt;
					
			&lt;tr&gt;
				&lt;td&gt;${post[0]}&lt;/td&gt;
				&lt;td&gt;${post[1]}&lt;/td&gt;
				&lt;td&gt;${post[2]}&lt;/td&gt;
				&lt;td&gt;${post[3]}&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/tbody&gt;
&lt;/c:forEach&gt;

答案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} &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


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 &lt;%@ page isELIgnored=&quot;false&quot;%&gt; evaluate EL

2) Create List&lt;List&gt; and add.

3) Set pageContext.setAttribute(&quot;list&quot;, finalList);

   
    &lt;!DOCTYPE html&gt;
    &lt;%@ page isELIgnored=&quot;false&quot;%&gt;
    &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html&quot;%&gt;
    &lt;%@ page import=&quot;java.util.*&quot;%&gt;
    &lt;%@taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;
    
    &lt;html&gt;
    &lt;head&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&gt;
    &lt;title&gt;List&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    	&lt;h1&gt;List&lt;/h1&gt;
    	&lt;%
    		List&lt;String&gt; list1 = Arrays.asList(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;);
    		List&lt;Integer&gt; list2 = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    		List&lt;List&gt; finalList = new ArrayList&lt;&gt;();
    		finalList.add(list1);
    		finalList.add(list2);
    		pageContext.setAttribute(&quot;list&quot;, finalList);
    	%&gt;
    	&lt;c:forEach items=&quot;${list}&quot; var=&quot;post&quot; varStatus=&quot;theCount&quot;&gt;
    		&lt;p&gt;List ${theCount.count}&lt;/p&gt;
    		&lt;c:forEach items=&quot;${post}&quot; var=&quot;value&quot; varStatus=&quot;cell&quot;&gt;
    			${value} &amp;nbsp;
    		&lt;/c:forEach&gt;
    	&lt;/c:forEach&gt;
    &lt;/body&gt;
    &lt;/html&gt;

//Output//

List
List 1

A   B   C   D   E   F   G  
List 2

1   2   3   4   5   6   7  


</details>



huangapple
  • 本文由 发表于 2020年3月16日 14:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/60701294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定