Java/JSTL/Loop

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

Count to 10 Example: tracking even and odd

   <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>

Count to 10 Example using JSTL

   <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>

By Twos

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

</html>


      </source>
   
  
 
  



JSTL: another for each and status

   <source lang="java">

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

 synchronized (pageContext) {
   String[] names = {"Joe", "Rosy", "Petter", "Rob"};
   pageContext.setAttribute("names", names, PageContext.PAGE_SCOPE);
 }

%> <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 element within the collection. 
<c:forEach var="currentName" items="${pageScope.names}" varStatus="status"> Family member #<c:out value="${status.count}" /> is <c:out value="${currentName}" />
</c:forEach> </body>

</html>


      </source>
   
  
 
  



JSTL: Conditional Support -- Simple Conditional Execution Example

   <source lang="java">

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

 <title>JSTL: Conditional Support -- Simple Conditional Execution Example</title>

</head> <body bgcolor="#FFFFFF">

Loop in JSTL

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

</body> </html>

      </source>
   
  
 
  



JSTL For Each

   <source lang="java">

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

 <head>
   <title>Reading RSS</title>
 </head>
 <body>
   <c:import var="news"
   url="http://www.wired.ru/news_drop/netcenter/netcenter.rdf" />
   <x:parse var="doc" xml="${news}" />
<x:forEach var="story" select="$doc/rss/channel/item">
         
           
             <x:out select="$doc/rss/channel/title" />
           
         
         
<x:out select="$doc/rss/channel/pubDate" />
         <x:out select="$doc/rss/channel/description" />



JSTL: for each and scoped variable

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="Joe, Zhou" 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"
   >
     Family member #<c:out value="${status.count}" /> is
       <c:out value="${currentName}" /> 
</c:forEach> </body>

</html>

      </source>
   
  
 
  



JSTL: for each and status

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="Joe, Rob, Rosy, Sissi" 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: for each 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" %> <%!

 String[] names = { "Joe", "Rosy", "Sissi", "Sun" };
 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>
   
  
 
  



JSTL Form Value and ForEach Loop

   <source lang="java">

<html>

 <head>
   <title>Page Data Example</title>
 </head>
 <body>
<form method="POST" action="params2.jsp"> </form>
           First Name
           <input type="text" name="first" size="40" />
           Last Name
           <input type="text" name="last" size="40" />
           Address
           <input type="text" name="address" size="40" />
           City
           <input type="text" name="city" size="20" />
           State
           <input type="text" name="state" size="20" />
           ZIP
           <input type="text" name="zip" size="20" />
           <input type="submit" value="Submit" name="action" />
           <input type="reset" value="Reset" name="action" />
 </body>

</html> ///////////////////////////////////////////////////////////////////////////////////// //File: params2.zip

      </source>
   
  
 
  



JSTL: fortokens

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="Joe:Petter;Ryan|John" scope="page" /> <html>

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

</html>


      </source>
   
  
 
  



JSTL Tag collaboration with a fixed 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>