简体   繁体   English

如何遍历JSP中的对象列表

[英]How to iterate through list of objects in jsp

Hi friends i am trying iterate through a list that contains list of objects I am trying to iterate through that list . 嗨,朋友,我正尝试遍历包含要尝试遍历该对象的对象列表的列表。 each element of the list is a 'object'. 列表中的每个元素都是一个“对象”。 I accessing the variables of that 'object' with the help of c:forEach . 我借助c:forEach访问该“对象”的变量。 I am getting the values. 我正在获取价值。 but the same again and again . 但一次又一次相同。 I dont know if i am doing any mistake while adding objects to list in the servlet or in the jsp while iterating through them. 我不知道在迭代对象时将对象添加到servlet或jsp中的列表时是否出错。

Here is my servlet code 这是我的servlet代码

FileInfo fileInfo = new FileInfo();
            List<FileInfo> fileInfoList = new ArrayList<FileInfo>();

            HashMap<String, String> uploadHistory = new HashMap<String, String>();

            while(rs.next()) {

                fileInfo.setFile_id(rs.getString("file_id"));
                fileInfo.setFile_name(rs.getString("file_name"));
                fileInfo.setUpload_time(rs.getString("upload_time"));
                fileInfo.setUsername(rs.getString("username"));     

                fileInfoList.add(fileInfo);

                uploadHistory.put(rs.getString("file_name"),rs.getString("upload_time"));
            }

            request.setAttribute("uploadHistory",uploadHistory);
            request.setAttribute("fileInfo",fileInfo);
            request.setAttribute("fileInfoList", fileInfoList);
            RequestDispatcher rd = request.getRequestDispatcher("/UploadHistoryJsp");
            rd.forward(request, response);

Here is my jsp 这是我的jsp

<div class="panel-body">
                    <table id="uploadHistoryTable" class="table">
                        <thead>
                            <tr>
                            <th><strong>File Name</strong></th>
                            <th><strong>Date & Time</strong></th>
                            </tr>
                        </thead>

                        <tbody>

                            <c:forEach var="uploaded" items="${fileInfoList}">
                            <tr>

                                <td><a href="${pageContext.request.contextPath}/DownloadServlet?f=${uploaded.file_name}&p=${uploaded.username}">${uploaded.file_name}</a></td>
                                <td>${uploaded.upload_time}</td>
                            </tr>
                            </c:forEach>
                        </tbody>
                    </table>

                </div>
            </div>
        </div><!-- panel-body -->

Please help . 请帮忙 。 Sorry for any confusion. 抱歉给您带来任何混乱。

Move this line: FileInfo fileInfo = new FileInfo(); 移动这一行: FileInfo fileInfo = new FileInfo(); within the while loop: while循环内:

while(rs.next()) {
FileInfo fileInfo = new FileInfo();
....

As is, you are creating only one instance and keep updating the same. 照原样,您仅创建一个实例,并继续对其进行更新。 Most likely you keep on seeing the last element you are pulling from the database multiple times. 您很可能会多次看到要从数据库中提取的最后一个元素。

You are adding same object again and again. 您一次又一次地添加相同的对象。 You need to initiate FileInfo inside while loop 您需要在while循环中启动FileInfo

  while(rs.next()) {
     FileInfo fileInfo= new FileInfo();
          .....
     fileInfoList.add(fileInfo)
  }

The problem with your code is you're always updating a single instance of FileInfo through fileInfo variable, so in each iteration you're just overwriting a single object's state. 代码的问题是,您总是通过fileInfo变量来更新FileInfo的单个实例,因此在每次迭代中,您都只是覆盖单个对象的状态。

   FileInfo fileInfo = new FileInfo();
   --------
    while(rs.next()) {
      // updating fileInfo from result set
   }

Move that FileInfo into the while loop like below 将该FileInfo移入while循环,如下所示

   while(rs.next()) {
      FileInfo fileInfo = new FileInfo();
      // update new fileInfo from result set and add to fileInfoList
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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