Java Tutorial/JSTL/ForEach

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

Check Even Odd Number in ForEach Loop

<%@ 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>
    <table border="0">
      <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>
        <tr>
          <td width="200" bgcolor="<c:out value="${color}"/>"> 
          <c:out value="${i}" />
          </td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>





JSTL ForEach Loop

<%@ 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}" />
      <br />
    </c:forEach>
  </body>
</html>





JSTL ForEach Loop with Begin/End Step

<%@ 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>
    <h1>The forEach tag exposes a scoped variable called "count", which
    is the position of the current iteration of the collection.</h1>
    <h2>(Note, it is <i>not</i> the position of the element in the
        underlying collection)</h2>
    <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}" /> <br />
    </c:forEach>
  </body>
</html>





JSTL ForEach Loop With Step

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Count to 10 Example(using JSTL)</title>
  </head>
  <body>
    <table border="1">
   <tr>
        <td>
          <h3>By Twos</h3>
          <c:forEach var="i" begin="2" end="10" step="2">
            <c:out value="${i}" />
            <br />
          </c:forEach>
        </td>
        <td valign="top">&nbsp;
        </td>
      </tr>
    </table>
  </body>
</html>





JSTL ForEach Status Count

<%@ 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}" /> <br />
    </c:forEach>
  </body>
</html>





JSTL Integer Controlled Loop

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Count to 10 Example(using JSTL)</title>
  </head>
  <body>
    <table border="1">
      <tr>
        <td valign="top">
          <h3>From 1 to 10</h3>
          <c:forEach var="i" begin="1" end="10">
            <c:out value="${i}" />
            <br />
          </c:forEach>
        </td>
        <td valign="top">
          <h3>From 10 to 1</h3>
          <c:forEach var="i" begin="1" end="10">
            <c:out value="${11-i}" />
            <br />
          </c:forEach>
        </td>
      </tr>
    </table>
  </body>
</html>





Reference Array by Index

<%@ 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>
    <H1>List of people</H1>
    <TABLE BORDER="1">
      <TH>Name</TH>
      <c-rt:forEach var="person" items="<%= names %>">
        <TR>
          <TD><c:out value="${person}"  /></TD>
          <TD><c:out value="${ages[i]}" /></TD>
        </TR>
      </c-rt:forEach>
    </TABLE>
  </BODY>
</HTML>





Use ForEach to Loop Through ArrayList

<%@ 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:
    <table>
      <TH>Name</th>
      <TH>Id</th>
      <c:forEach items="${authors}" var="current">
        <tr>
          <td><c:out value="${current.name}" /><td>
          <td><c:out value="${current.id}" /><td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>





Use For Each to Loop Through Comma Delimited String

<%@ 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" />
    <table border="0">
      <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>
        <tr>
          <td width="200" bgcolor="<c:out value="${color}"/>"> 
          <c:out value="${i}" />
          </td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>





Use JSTL ForEach Tag to Loop Through a String

<%@ 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" };
%>
  <!-- application.setAttribute("bikeList", bikes); -->  
<HTML>
  <HEAD><TITLE>JSTL "forEach" tag</TITLE></HEAD>
  <BODY>
    Here are my bikes:
    <UL>
      <c-rt:forEach var="bike" items="<%= bikes %>">
        <LI><c:out value="${bike}" />
      </c-rt:forEach>
    </UL>
  </BODY>
</HTML>





Use JSTL ForEach to Loop through a Vector

<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<html>
  <head>
    <title>Tag Plugin Examples: forEach</title>
  </head>
  <body>
    <h1>Tag Plugin Examples - &lt;c:forEach></h1>
    <hr>
    <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);
    %>
    <h3>Iterating over a Vector</h3>
    <c:forEach items="${vector}" var="item" >
      <c:out value="${item}"/>
    </c:forEach>

  </body>
</html>