Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use <c:foreach

Posted on 2007-03-20
1
Medium Priority
?
822 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 2000 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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

660 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