Solved

How to use <c:foreach

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now