在迭代多个对象时使用JSTL显示JSP表

问题描述 投票:0回答:1

我实际上仍然是这个java ee的首发,我已经设法得到了相当远的...

我会尽力解释我的问题,这是我能忍受的最简单的方式.. :)

我正在使用我的Servlet“MajTablesController.java”从多个对象中检索对象信息(name,ref等...)[目标是显示将所有这些信息收集到一个对象中的表]

使用相同的servlet i将列表作为名为“Resultat”的会话属性发送,其中包含自己之前提到的不同对象的列表。

总而言之,My List Resultat(由我的JSP接收)包含4个ListObjects。

问题是在创建表时,我需要使用多个循环才能从不同的对象中检索数据,无论我如何放置forEach标签,我都无法设法以正确的方式获取表格(请参阅图片) ,我最终得到一行或一列的所有信息......

为了帮助您更清楚地查看正在发生的事情,以下是我使用此代码获得的示例:

码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.estia.tai.UsersBeanModel"%>
<%@ page import="com.estia.tai.EchantillonBeanModel"%>

<%@ page import="java.sql.*"%>
<%@ page import="com.estia.tai.BDDConnect"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<link rel="stylesheet" type="text/css" media="screen"
href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen"
    href="../css/styles.css">
    <script src="../js/scripts.js"></script>
    <script src="https://code.jquery.com/jquery-1.10.2.js"
        type="text/javascript"></script>
    <link rel="stylesheet"
        href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">
        <script
            src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>
        <head>
        </head>
        <div class="container-fluid">
            <div class="col-md-12">
                <div class="table-responsive table-dark" id="preptable">

                    <table class="table table-bordered table-hover text-center"
                        data-toggle="" data-search="false" data-filter-control="true"
                        data-show-export="true" data-click-to-select="true">
                        <thead>
                            <tr class="">
                                <th data-field=id data-filter-control="select"
                                    data-sortable="true">Id Commande</th>
                                <th data-field=echantillons data-filter-control="select"
                                    data-sortable="true">Echantillons</th>
                                <th data-field=conditionnement data-filter-control="select"
                                    data-sortable="true">Conditionnement</th>
                                <th data-field=transporteur data-filter-control="select"
                                    data-sortable="true">Transporteur</th>
                                <th data-field=etat data-filter-control="select"
                                    data-sortable="true">Etat</th>
                                <th data-field=date data-filter-control="select"
                                    data-sortable="true">Date</th>
                                <th data-field=client data-filter-control="select"
                                    data-sortable="true">Clientss</th>
                            </tr>

                        </thead>







                        <tbody>
                            <c:forEach items="${Resultat}" var="variable">

                                <tr>
                                    <td><a href="../CommandeVueController?id=${variable.id}">${variable.id}</a></td>
                                    <td>${variable.echantillonRef}</td>
                                    <td>${variable.conditionnement}</td>
                                    <td><a
                                        href="../TransporteurVueController?id=${variable.transporteurId}">${variable.transporteur}</a></td>
                                    <td>${variable.etat}</td>
                                    <td>${variable.date}</td>
                                    <td>${variable.client}</td>



                                </tr>
                            </c:forEach>


                        </tbody>


                    </table>
                </div>
                <!--end of .table-responsive-->

                <script>
                    var $table = $('#preptable');
                    $(function() {
                        $('#toolbar').find('select').change(function() {
                            $table.bootstrapTable('refreshOptions', {
                                exportDataType : $(this).val()
                            });
                        });
                    })

                    var trBoldBlue = $("preptable");

                    $(trBoldBlue).on("click", "tr", function() {
                        $(this).toggleClass("bold-blue");
                    });

                    $(document)
                            .ready(
                                    function() {
                                        td_array = document
                                                .getElementsByTagName("td");
                                        check_prep = "En preparation";

                                        for (i = 0; i < td_array.length; i++) {

                                            if (td_array[i].textContent == check_prep) {
                                                td_array[i].style.backgroundColor = "#FF8C00";
                                            }
                                            ;
                                        }
                                        ;
                                    });
                </script>
            </div>
        </div>

TablePrep.jsp它实际显示的内容:https://screenshotscdn.firefoxusercontent.com/images/2e48ceaf-3cd6-422a-a21c-b01d215b5d0e.png

目标是让下面列表中的每个项目都有自己的行(就像表格一样)

请注意,prepTable方法(从其他对象收集和协调信息)在其getters方法中发送Lists,这解释了为什么我们在表中有列表。

!ServletSide

          BDDConnect connexionBDDModele = new BDDConnect();
        Connection connection = connexionBDDModele.getConnexion();

    List<PrepTableVueModel> prepTableList = new     ArrayList<PrepTableVueModel>();
    int TransporteurId = 0;


    if (request.getSession().getAttribute("p") == "p") {


        PrepTableVueModel prepTable = new PrepTableVueModel();




        //request.getSession().setAttribute("commandeResultat", CommandeDAOModel.lireList());
        //request.getSession().setAttribute("echantillonResultat", EchantillonDAOModel.lireListe());
        //request.getSession().setAttribute("conditionnementResultat", ConditionnementDAOModel.lireListe());
        //request.getSession().setAttribute("clientResultat", ClientDAOModel.lireListe());
        //request.getSession().setAttribute("transporteurResultat", TransporteurDAOModel.lireListe());


        prepTable.addCommande(CommandeDAOModel.lireList());
        for (int i = 0 ; i < CommandeDAOModel.lireList().size(); i++) {

            prepTable.addEchantillon(EchantillonDAOModel.lireListeEch(CommandeDAOModel.lireList().get(i).getId()));

        }

        for (int i = 0 ; i< prepTable.EchantillonList.size(); i++) {

            prepTable.addEchantillonRef(EchantillonDAOModel.lireListeEchFinal(prepTable.EchantillonList.get(i)));

        }

        for (int i =0; i < prepTable.EchantillonListRef.size(); i++) {


        }

        //prepTable.addConditionnement(ConditionnementDAOModel.lireListe());
        //prepTable.addClient(ClientDAOModel.lireListe());
        //prepTable.addTransporteur(TransporteurDAOModel.lireListe());

        System.out.println(prepTable.getId() + "id dans le maj ");
        System.out.println(prepTable.getEchantillon() + "ech dans le maj ");


        prepTableList.add(prepTable);


        request.getSession().setAttribute("Resultat", prepTableList);

        request.getRequestDispatcher("/panel/tableprep.jsp").forward(request, response);

我试过,而不是从PrepTable返回元素列表,返回单个元素但没有成功..

java html jsp html-table jstl
1个回答
0
投票

我已经成功地做到了,诀窍是向JSP页面发送一个额外的字符串属性,指示我拥有的元素数量(设置循环)代码变为:

大段引用

<tbody>
                            <c:forEach begin="0" end="${ResultNumberCom}" varStatus="loop">
                                <c:forEach items="${Resultat}" var="variable">

                                    <tr>
                                        <td><a href="../CommandeVueController?id=${variable.id[loop.index]}">${variable.id[loop.index]}</a></td>
                                        <td>${variable.echantillonRef[loop.index]}</td>
                                        <td>${variable.conditionnement[loop.index]}</td>
                                        <td><a
                                            href="../TransporteurVueController?id=${variable.transporteurId[loop.index]}">${variable.transporteur[loop.index]}</a></td>
                                        <td>${variable.etat[loop.index]}</td>
                                        <td>${variable.date}[loop.index]</td>
                                        <td>${variable.client}</td>



                                    </tr>
                                </c:forEach>
                            </c:forEach>
© www.soinside.com 2019 - 2024. All rights reserved.