foreach如何在JSP中用于列表列表

时间:2020-03-16 06:30:08

标签: java jsp foreach jstl nested-loops

[“ 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是四个数组列表的组合

如何将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);
            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);
    }

2 个答案:

答案 0 :(得分:0)

我得到了我问题的答案。

在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);

现在我有一个简单的列表来传递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>

答案 1 :(得分:-1)

1)使用<%@页isELIgnored =“ false”%>评估EL

2)创建列表并添加。

3)设置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>

//输出//

列表 清单1

A B C D E F G
清单2

1 2 3 4 5 6 7