简体   繁体   中英

Display JSP Table using JSTL while iterating multiple objects

I am actually still a starter in this java ee stuff, and I have managed to get quite far still..

I will try to explain my problem the simplest way I can so bear with me.. :)

I am using my Servlet "MajTablesController.java" to retrieve object info ( name, ref etc.. ) from multiple objects [ The Goal is to show a Table that gathers all of this information into one object ]

Using the same servlet i Send a List as a Session Attribute named " Resultat " which contains itself list of the different objects mentionned before.

So to sum up, My List Resultat ( which is received by my JSP ) contains 4 ListObjects.

The problem is when creating the table, i need to use multiple loops in order to retrieve the data from the different objects, no matter how i place the forEach tags, i can't manage to get the table the right way ( see pics ), and I end up either getting all of the info in one line or one column...

To help you visualise more clearly what is happening, here is an example of what i Am getting with this code :

Code:

<%@ 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/bootstrap-table@1.13.4/dist/bootstrap-table.min.css">
        <script
            src="https://unpkg.com/bootstrap-table@1.13.4/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 What it actually shows : https://screenshotscdn.firefoxusercontent.com/images/2e48ceaf-3cd6-422a-a21c-b01d215b5d0e.png

The Goal is to have each item of the lists below have its own Row ( just like a table )

Notice that the prepTable methods ( which gather and coordinate info from other objects ) sends Lists in their getters methods, which explains why we have lists in the table.

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

I have tried to, Instead of returning Lists of elements from PrepTable, to return individual elements but without success..

I have managed to get it right, The trick was to send an additionnal string attribute to the JSP page indicating the number of elements i had ( to setup the loop ) The code becomes :

Blockquote

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM