Проблема с таблицей JSP / Java - PullRequest
1 голос
/ 14 июля 2010

У меня есть таблица, которая заполняется в зависимости от количества машин.Если количество автомобилей равно 1, это даст мне 1 строку (где 5 атрибутов расположены в 5 столбцах).Если количество машин равно 2, это даст мне 2 строки (те же 5 атрибутов) и так далее.Теперь мне нужно разбить таблицу на столько машин, чтобы на каждой машине был только один ряд.Мне нужно сделать это в JSP и пытаться использовать тег <c:choose> или <c:if>, но не работает.Пожалуйста, помогите

Ответы [ 2 ]

2 голосов
/ 14 июля 2010

Вам нужно <c:forEach> здесь.С его помощью вы можете перебирать любой List<T> и печатать <tr> на каждой итерации.Предполагая, что вы заполнили List<Car> и поместили его в область EL как ${cars}, вот пример:

<table>
    <c:forEach items="${cars}" var="car">
        <tr>
            <td>${car.make}</td>
            <td>${car.model}</td>
            <td>${car.type}</td>
            <td>${car.color}</td>
            <td>${car.price}</td>
        </tr>
    </c:forEach>
</table>

См. Также:

0 голосов
/ 27 августа 2011
<html>
<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
    function start() {
        // get the reference for the body
        var body = document.getElementsByTagName("body")[0];

        // creates a <table> element and a <tbody> element
        var tbl     = document.createElement("table");
        var tblBody = document.createElement("tbody");

        // creating all cells
        for (var j = 0; j < 2; j++) {
            // creates a table row
            var row = document.createElement("tr");

            for (var i = 0; i < 2; i++) {
                // Create a <td> element and a text node, make the text
                // node the contents of the <td>, and put the <td> at
                // the end of the table row
                var cell = document.createElement("td");
                var cellText = document.createTextNode("cell is row "+j+", column "+i);
                cell.appendChild(cellText);
                row.appendChild(cell);
            }

            // add the row to the end of the table body
            tblBody.appendChild(row);
        }

        // put the <tbody> in the <table>
        tbl.appendChild(tblBody);
        // appends <table> into <body>
        body.appendChild(tbl);
        // sets the border attribute of tbl to 2;
        tbl.setAttribute("border", "2");
    }
</script>
</head>
<body onload="start()">
</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...