Java Tutorial/JSTL/ForEach

Материал из Java эксперт
Перейти к: навигация, поиск

Check Even Odd Number in ForEach Loop

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %> <html>

 <head>
   <title>Count to 10 Example(tracking even and odd)</title>
 </head>
 <body>
<c:forEach var="i" begin="1" end="10" varStatus="status"> <jsp:useBean id="status" type="javax.servlet.jsp.jstl.core.LoopTagStatus" /> <c-rt:choose> <c-rt:when test="<%=status.getCount()%2==0%>"> <c:set var="color" value="#eeeeee" /> </c-rt:when> <c-rt:otherwise> <c:set var="color" value="#dddddd" /> </c-rt:otherwise> </c-rt:choose> <td width="200" bgcolor="<c:out value="${color}"/>"> <c:out value="${i}" /> </td> </c:forEach> </table> </body> </html></source>

JSTL ForEach Loop

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Count to 10 Example(using JSTL)</title>
 </head>
 <body>
   <c:forEach var="i" begin="1" end="10" step="1">
     <c:out value="${i}" />
     
</c:forEach> </body>

</html></source>





JSTL ForEach Loop with Begin/End Step

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="Peter, Pat, Mark, Tracy" scope="page" /> <html>

 <head>
   <title>forEach and status</title>
 </head>
 <body>

The forEach tag exposes a scoped variable called "count", which is the position of the current iteration of the collection.

(Note, it is not the position of the element in the underlying collection)

   <c:forEach items="${pageScope.names}"
              var="currentName"
              varStatus="status"
              begin="0"
              end="3"
              step="2"
   >
     Family member #<c:out value="${status.count}" /> is
       <c:out value="${currentName}" /> 
</c:forEach> </body>

</html></source>





JSTL ForEach Loop With Step

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Count to 10 Example(using JSTL)</title>
 </head>
 <body>

By Twos

         <c:forEach var="i" begin="2" end="10" step="2">
           <c:out value="${i}" />
           
</c:forEach>
 
 </body>

</html></source>





JSTL ForEach Status Count

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="A B C, D" scope="page" /> <html>

 <head>
   <title>forEach and status</title>
 </head>
 <body>
   <c:forEach items="${pageScope.names}"
              var="currentName"
              varStatus="status"
   >
     Family member #<c:out value="${status.count}" /> is
       <c:out value="${currentName}" /> 
</c:forEach> </body>

</html></source>





JSTL Integer Controlled Loop

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Count to 10 Example(using JSTL)</title>
 </head>
 <body>

From 1 to 10

         <c:forEach var="i" begin="1" end="10">
           <c:out value="${i}" />
           
</c:forEach>

From 10 to 1

         <c:forEach var="i" begin="1" end="10">
           <c:out value="${11-i}" />
           
</c:forEach>
 </body>

</html></source>





Reference Array by Index

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/core_rt" prefix="c-rt" %> <%!

 String[] names = { "A","B", "C", "D" };
 int[]    ages  = { 29, 8, 6, 5};

%> <HTML>

 <HEAD><TITLE>JSTL "forEach" tag</TITLE></HEAD>
 <BODY>

List of people

     <c-rt:forEach var="person" items="<%= names %>">
</c-rt:forEach>
Name
<c:out value="${person}" /> <c:out value="${ages[i]}" />
 </BODY>

</HTML></source>





Use ForEach to Loop Through ArrayList

   <source lang="java">

<%@ page language="java" contentType="text/html" %> <%@ page import="java.util.*" %> <%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <%

 // Create an ArrayList with test data
 ArrayList list = new ArrayList();
 Map author1 = new HashMap();
 author1.put("name", "A");
 author1.put("id", new Integer(1));
 list.add(author1);
 Map author2 = new HashMap();
 author2.put("name", "B");
 author2.put("id", new Integer(2));
 list.add(author2);
 Map author3 = new HashMap();
 author3.put("name", "C");
 author3.put("id", new Integer(3));
 list.add(author3);
 pageContext.setAttribute("authors", list);

%> <html>

 <head>
   <title>Search result: Authors</title>
 </head>
 <body bgcolor="white">
   Here are all authors matching your search critera:
     <c:forEach items="${authors}" var="current">
</c:forEach>
Name Id
<c:out value="${current.name}" /> <c:out value="${current.id}" />
 </body>

</html></source>





Use For Each to Loop Through Comma Delimited String

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %> <html>

 <head>
   <title>Count to 10 Example(tracking even and odd)</title>
 </head>
 <body>
   <c:set var="days" value="A,B,C,D,E,F,G" />
<c:forEach var="i" items="${days}" varStatus="status"> <jsp:useBean id="status" type="javax.servlet.jsp.jstl.core.LoopTagStatus" /> <c-rt:choose> <c-rt:when test="<%=status.getCount()%2==0%>"> <c:set var="color" value="#cccccc" /> </c-rt:when> <c-rt:otherwise> <c:set var="color" value="#dddddd" /> </c-rt:otherwise> </c-rt:choose> <td width="200" bgcolor="<c:out value="${color}"/>"> <c:out value="${i}" /> </td> </c:forEach> </table> </body> </html></source>

Use JSTL ForEach Tag to Loop Through a String

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/core_rt" prefix="c-rt" %> <%

 String[] bikes = { "A", "B", "C" };

%>

<HTML>

 <HEAD><TITLE>JSTL "forEach" tag</TITLE></HEAD>
 <BODY>
   Here are my bikes:
    <c-rt:forEach var="bike" items="<%= bikes %>">
  • <c:out value="${bike}" /> </c-rt:forEach>
 </BODY>

</HTML></source>





Use JSTL ForEach to Loop through a Vector

   <source lang="java">

<html>

 <head>
   <title>Tag Plugin Examples: forEach</title>
 </head>
 <body>

Tag Plugin Examples - <c:forEach>


   <font color="#000000"/>
   </br>
   <%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
   <%@ page import="java.util.Vector" %>
   <% Vector v = new Vector();
 v.add("One"); v.add("Two"); v.add("Three"); v.add("Four");
 pageContext.setAttribute("vector", v);
   %>

Iterating over a Vector

   <c:forEach items="${vector}" var="item" >
     <c:out value="${item}"/>
   </c:forEach>
 </body>

</html></source>