Solved

How to use <c:foreach

Posted on 2007-03-20
1
814 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 38
hibernate jars 4 57
Crystal Reports Licensing Questions 4 59
jsp error 6 43
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 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