Solved

How to use <c:foreach

Posted on 2007-03-20
1
816 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.
Suggested Courses

739 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