Friday, 15 August 2014

java - How to iterate through list of objects in jsp -


Hi guys, I am trying again through the list which contains the list of items, which I am trying to be repeated through that list. Each element in the list is a 'object', I am accessing the variable of 'object' with the help of: c: forEach I am getting the price, but once again I do not know if I have the objects in the servlet or JSS I am making a mistake while adding to the list.

This is my servlet code

  FileInfo fileInfo = new file Infos (); & Lt; FileInfo & gt; FileInfoList = New ArrayList & lt; FileInfo & gt; (); Hashmap & lt; String, string & gt; UploadHistory = New Hashmop & lt; String, string & gt; (); While (rsnext ()) {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 ("user name")); 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); Request Dispatcher rd = request.getRequestDispatcher ("/ UploadHistoryJsp"); Rd.forward (request, response);  

Here's my JSP

  & lt; Div class = "panel-body" & gt; & Lt; Table id = "uploadHistoryTable" class = "table" & gt; & Lt; Thead & gt; & Lt; TR & gt; & Lt; Th & gt; & Lt; Strong & gt; File name & lt; / Strong> & Lt; / Th & gt; & Lt; Th & gt; & Lt; Strong & gt; Date & amp; Time & lt; / Strong> & Lt; / Th & gt; & Lt; / TR & gt; & Lt; / Thead & gt; & Lt; Tbody & gt; & Lt; C: forEach var = "uploaded" items = "$ {fileInfoList}" & gt; & Lt; TR & gt; & Lt; Td> & Lt; A href = "$ {pageContext.request.contextPath} / downloadServelet? F = $ {uploaded.file_name} & amp; p = $ {uploaded.username}" & gt; $ {Uploaded.file_name} & lt; / A & gt; & Lt; / TD & gt; & Lt; TD & gt; $ {Uploaded.upload_time} & lt; / TD & gt; & Lt; / TR & gt; & Lt; / C: foreach & gt; & Lt; / Tbody & gt; & Lt; / Table & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt ;! - Panel-body - & gt;  

Please help forgive for any confusions.

Move this line: FileInfo fileInfo = new FileInfo (); inside while loop:

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

As you are just making an example and keep updating it. Most likely you will be watching the last element pulling the database several times.


No comments:

Post a Comment