We help IT Professionals succeed at work.

Issue with Arraylist and JSTL

aman0711
aman0711 asked
on
801 Views
Last Modified: 2013-11-24
Hi Experts,
   
                  I Have an arraylist with size 10. This arraylist is populated from the DB.
 
                  I am using JSTL to display the contents of this ArrayList on the JSP using this code:

   <ul>
         <c:forEach var="groups" items="${groups}">
                  <li><a href="<c:out value='${groups}'/>"><c:out value='${groups}'/></a></li>
            </c:forEach>
  </ul>

             but instead of displaying 10 items, its displaying lot more than that. (ArrayList contains few nulls as well included in total count of 10)

            Below is the method which populates the ArrayList and the snapshot of outpout I am getting.
 
 public ArrayList getGroups() throws SQLException{
	    	
	    	  DbJDBC db = new DbJDBC();
	    	  
	    	  Connection conn = db.getConnection();
	    	  
	    	  String sql = "SELECT GROUPS FROM VSLAND.SIWEB_HOME_TAB";
	    	  try {
				 stmt = conn.createStatement();
				
			    	rs = stmt.executeQuery(sql);
				
			    	groups = new ArrayList<String>(); 
					while (rs.next()) {
						
					   groups.add(rs.getString(1));
							  					   
					}
					
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
	            rs.close();
		        stmt.close();
		        conn.close();
		     
		    System.out.println("Size >>>>>>>>>>>" + groups.size());    
		        
		  return groups;   
	    	    	
	    }

Open in new window

jstl-snap.PNG
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
looks like you're adding extra data to your db somewhere, check whats actually in the db
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
hi Mick,

            this is my Table on the DB. everything looks fine :(

groups-table.PNG
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
actually loop should be more like this:

         
                 
  • ${groups}
  •            

    Author

    Commented:
    @ iaonton,

                   your code works and I am not even seeing the nulls :)

    Author

    Commented:
    Oops, didnt see your post ...

    what was wrong with my codE?
    Mick BarryJava Developer
    CERTIFIED EXPERT
    Top Expert 2010
    Commented:
    This one is on us!
    (Get your first solution completely free - no credit card required)
    UNLOCK SOLUTION

    Commented:
    Actually, the "var" name must be different than "items" name. Otherwise you end up displaying the whole list at once.

    Author

    Commented:
    Thanks iaonton,

                 I would have never known that var name needs to diff always

    Author

    Commented:
    but what is wrong with this code?

    The last list "aspects" is giving the same trouble.

    even though var name is diff from items

    	    <td class="ratio_25">
               					    <ul>
      									<c:forEach var="group" items="${groups}">
                  					    <li><a href="<c:out value='${group}'/>"><c:out value='${group}'/></a></li>
           								</c:forEach>
    								</ul>
         						    </td>
           							<td class="flat ratio_50">
                 				     <ul>
                            			<c:forEach var="applications" items="${applications}">
                  					    <li><a href="<c:out value='${applications}'/>"><c:out value='${applications}'/></a></li>
           								</c:forEach>
                                     </ul>
                    			   </td>
          						   <td class="ratio_25">
                  					 <ul>
                         			  <c:forEach var="aspect" items="${aspects}">
                  					    <li><a href="<c:out value='${aspects}'/>"><c:out value='${aspects}'/></a></li>
           								</c:forEach>
                   					 </ul>
         						   </td>
    

    Open in new window

    Commented:
    You use within the tag c:out the name of the collection (aspects) not of the var (aspect)

    Author

    Commented:
    oops sorry silly mistake on my part. Got it :)

    Author

    Commented:
    >> You use within the tag c:out the name of the collection (aspects) not of the var (aspect)


    <c:forEach var="aspect" items="${aspects}">
      <li><a href="<c:out value='${aspect}'/>"><c:out value='${aspect}'/></a></li>
      </c:forEach>

    Author

    Commented:
    The above code works , even though as you mentioned, we need to give the name of collection, not var

    Commented:
    Maybe I did not speak too well. You got the wrong result because you use within the tag c:out the name of the collection (aspects) instead of the var (aspect).

    Author

    Commented:
    Oh ok got it :)

    Thank you so much for your help :)

    Author

    Commented:
    Thanks folks :)
    CERTIFIED EXPERT

    Commented:
    Hi  amam,

    This answer is i already suggest u in our previews discussion ....
    check that my  in commands ...
    https://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSTL_Tags/Q_24895301.html#25808225
      <c:forEach var="group" items="${groups}">
                      <li><a href="<c:out value='${group}'/>"><c:out value='${group}'/></a></li>
             </c:forEach>
    Unlock the solution to this question.
    Join our community and discover your potential

    Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

    *This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    OR

    Please enter a first name

    Please enter a last name

    8+ characters (letters, numbers, and a symbol)

    By clicking, you agree to the Terms of Use and Privacy Policy.