Solved

How to use <c:foreach

Posted on 2007-03-20
1
813 Views
Last Modified: 2013-12-22
Hi Guys

I need help in how to use <C:foreach tag

Currrently I display my data from an array of string like this. Would anyone please show me how I can change this to JSTL, Thanks

------------------------------------------------

<%
                if(reportResult!=null)
                {
                 for(int i=0;i<reportResult.length;i++)
                 {String itArr[] =reportResult[i].split(",");
                 
                  %>  
                        <tr align="center">      
                        <TD><%if(null != itArr[0])out.write(itArr[0]); %></TD>
                        <TD><%if(null != itArr[1])out.write(itArr[1]); %></TD>
                        <TD><%if(null != itArr[2])out.write(itArr[2]); %></TD>                
                        </tr>
                        <% }}%>

-----------------------------------------------------------------------
0
Comment
Question by:huzefaq
1 Comment
 
LVL 1

Accepted Solution

by:
clim1219 earned 500 total points
ID: 18761937
Hi huzefaq,

Here's the code that you need:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%
String[] reportResult = {
    "Element 1,Element 2,Element 3,Element 4,Element 5",
    "Element 6,Element 7,Element 8,Element 9,Element 10"
};

request.setAttribute( "reportResult", reportResult );
%>

<html>

  <table border="1">
      <c:forEach var="elements" items="${reportResult}">

          <tr>

              <c:set var="delimiter" value=","/>
              <c:set var="itArr" value="${fn:split(elements,delimiter)}"/>
             
              <c:forEach var="element" items="${itArr}">
                  <td>${element}</td>
              </c:forEach>

          </tr>
      </c:forEach>
  </table>

</html>


Basically the codes uses two forEach constructs. First in iterating the reportResult array, the second is on the splitted array values. Also, it uses the fn:split function to parse the string.

You need the JSTL jar for this to run. It can be downloaded from here: http://repo1.maven.org/maven2/javax/servlet/jstl/1.1.2/jstl-1.1.2.jar



Cheers!

Chris
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
Java exception bubble up 2 21
Setup GlassFish 4 21
maven not picking latest jar instead picking old jar from .m2 12 17
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question