Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: forEach tag

  1. #1
    Join Date
    Jan 2007
    Posts
    25

    Default forEach tag

    Can some one tell me how to write this without the scriptlets .. using forEach tag ? variable firstDay should be read from the formbackingObject ..

    Code:
    				<%	
    				for (int i = 0; i < firstDay ; i++) {
    					     if (i==0){ %>
    					      <td align='center' width='10%' valign='top' bgcolor="${calDisplayData.noDaysColor}">
    						      &nbsp;
    					      </td>			
    					     <%} else {  %>
    							<td align='center' width='10%' valign='top' bgcolor="">&nbsp;</td>
    						<% } 
    				  	}
    				%>
    			<%

  2. #2
    Join Date
    Dec 2006
    Location
    Normal, Illinois
    Posts
    277

    Default

    Try this...Need to make sure your are using the JstlView

    Code:
    <c:forEach var="x" begin="0" end="${firstDay}" step="1" varStatus="status">
     
    <c:if test="${status.first}">
    <td align='center' width='10%' valign='top' bgcolor="${calDisplayData.noDaysColor}">
    					      &nbsp;
    					      </td>			
    </c:if>
    <c:if test="${!status.first}">
      <td align='center' width='10%' valign='top' bgcolor="">&nbsp;</td>
    </c:if> 
    </c:forEach>
    My Jstl is a little rough, but I think this should do trick...

    Can do a <c:out value="${firstDay}"/> to make sure that your first day value is making it to your jstl tag(s)
    Caleb Washburn

  3. #3
    Join Date
    Jan 2007
    Posts
    25

    Default

    Why should i use JSTLView ?

  4. #4
    Join Date
    Dec 2006
    Location
    Normal, Illinois
    Posts
    277

    Default

    Check out this link from the doc...

    http://static.springframework.org/sp....html#view-jsp

    Specifically this section.

    14.2.2. 'Plain-old' JSPs versus JSTL

    When using the Java Standard Tag Library you must use a special view class, the JstlView, as JSTL needs some preparation before things such as the i18N features will work.
    Caleb Washburn

  5. #5
    Join Date
    Jan 2007
    Posts
    25

    Default

    It worked :-) Thank You

    One more question .

    How do i iterate through nested list using the forEach tag ..

    For example i have a list which contains object of type list .. the inside list contains the object of type XXXXXX .. now in my jsp i want iterate through the first list .. get the list inside it ... Iterate through the second list .. get the projects from it ..

    Thank You

  6. #6
    Join Date
    Dec 2006
    Location
    Normal, Illinois
    Posts
    277

    Default

    If I understand you correctly I believe you would do this.

    Code:
    <c:forEach var="secondList" items="${firstList}">
      <c:forEach var="secondList_item" items="${secondList}">
        <c:out value="${secondList_item}"/>
       </c:forEach>
    </c:forEach>
    Where firstList is a list that contains another list (secondList) and secondList contains the objects you want.
    Caleb Washburn

  7. #7
    Join Date
    Jan 2007
    Posts
    25

    Default

    but my issue is i dont want to iterate through the list completely . i want to do stop at a certain number ..
    Here is what i am trying to do in scriptlet

    Code:
    			for (int j = firstDay; j < 7; j++) {
          			List project = (List)releaseList.get(day);
    			    if (project != null && project.size() != 0) {
           				 out.println("<td width='10%' align='right' valign='top' bgcolor='"+ CalendarUtil.getWeekEndColor(year, month-1, day+1) + "'><table>");
      			         out.println("<tr width='100%'><td align='right' width='100%' valign='top'><b>"+ (day+1) + "</b></td></tr>");			        
        			     for (int k = 0; project != null && k < project.size(); k++) {
    					 	ReleaseScheduleData pi =(ReleaseScheduleData)project.get(k);
    			            if (pi.getReleaseType().equalsIgnoreCase("ecm freeze")) {
    			                out.println("<tr width='100%' align='left'><td width='100%' bgcolor='"
                		        + pi.getBackGrndColor() + "'><font color='#FF0000'>&nbsp;&nbsp;"
                        		+ pi.getReleaseName() + "&nbsp;&nbsp;</font></td></tr>");
    		              } else if (pi.getReleaseType().equalsIgnoreCase("content only freeze") || pi.getReleaseType().equalsIgnoreCase("combo freeze")
            			      || pi.getReleaseType().equalsIgnoreCase("code only freeze")) {
    			                out.println("<tr width='100%' align='left'><td width='100%' bgcolor='"
                		        + pi.getBackGrndColor() + "'><font color='#FF0000'>&nbsp;&nbsp;"
                        		+ pi.getReleaseName() + "&nbsp;&nbsp;</font></td></tr>");
    		              } else if (pi.getReleaseType().equalsIgnoreCase("Weekly Maintenance")) {
    		                out.println("<tr width='100%' align='left'><td width='100%' bgcolor='"
            	            + pi.getBackGrndColor() + "'><font color='#FF0000'>&nbsp;&nbsp;"
                	        + pi.getReleaseName() + "&nbsp;&nbsp;</font></td></tr>");
                  		}
            		}
           	 		out.println("</table></td>");
          		} else {
    		        out.println("<td width='10%' align='right' valign='top' bgcolor='"+ CalendarUtil.getWeekEndColor(year, month-1, day+1) + "'><b>" + (day + 1) + "</b></td>");
          		}

  8. #8
    Join Date
    Jan 2007
    Posts
    25

    Default

    And also i have to keep track of the index where i stopped the iteration .. because i have to start the next iteration from there ...

  9. #9
    Join Date
    Dec 2006
    Location
    Normal, Illinois
    Posts
    277

    Default

    Pretty sure you can do this....

    Code:
    <c:forEach var="project" items="${releaseList}" start="${firstDay}" stop="7" step="1" varStatus="status">
      <c:forEach var="pi" items="${project}">
        <c:out value="${pi}"/>
       </c:forEach>
    </c:forEach>
    The default forEach assumes start=0, stop=collection.size() and step=1

    To keep track of the index use the status variable (status.count). if you need this outside the scope of your for loop I believe you need to set it to a jstl variable.

    Code:
    <c:set var="name1" scope="page">
            line 1
            line 2
        </c:set>
    Hopefully this works.
    Last edited by cwash5; Jan 5th, 2007 at 05:07 PM.
    Caleb Washburn

  10. #10
    Join Date
    Jan 2007
    Posts
    25

    Default

    Okay i got it ..

    but i have this issue now


    Code:
    <c:forEach begin = "0" end = "7" items="${calDisplayData.projectList}" var="secondList">
    <c:if test="${fn:length(secondList) > 0}" >
    <td width='10%' align='right' valign='top' 
    bgcolor=CalendarUtil.getWeekEndColor(year, month-1, day+1) >>
    </td>
    </c:forEach>
    here bgcolor=CalendarUtil.getWeekEndColor(year, month-1, day+1) CalendarUtil is a java util class , year and month should be read from the command bean and day is the index

    How do i make this call to java ?? i need the syntax ..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •