Java/JSTL/Loop

Материал из Java эксперт
Версия от 06:17, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Count to 10 Example: tracking even and odd

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





Count to 10 Example using JSTL

<%@ 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>
      <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: another for each and status

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





JSTL: Conditional Support -- Simple Conditional Execution Example

<%@ 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">
<h3>Loop in JSTL</h3>
    <c:forEach var="i" begin="1" end="10" step="1">
      <c:out value="${i}" />
      <br />
    </c:forEach>

</body>
</html>





JSTL For Each

<%@ 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}" />
    <table border="1" width="100%">
      <tr bgcolor="blue">
        <td align="center">
          <font color="white" size="+2">
            <b>
              <x:out select="$doc/rss/channel/title" />
            </b>
          </font>
          <br />
          <font color="white" size="-2">
            <x:out select="$doc/rss/channel/pubDate" />
          </font>
        </td>
      </tr>
      <tr>
        <td valign="top">
          <x:out select="$doc/rss/channel/description" />
        </td>
      </tr>
      <x:forEach var="story" select="$doc/rss/channel/item">
        <tr bgcolor="blue">
          <td align="center">
            



== JSTL: for each and scoped variable ==






   
  <!-- start source code -->
   
    <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>
    <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"
    >
      Family member #<c:out value="${status.count}" /> is
        <c:out value="${currentName}" /> <br />
    </c:forEach>
  </body>
</html>





JSTL: for each and status

<%@ 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>
    <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: for each loop

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





JSTL Form Value and ForEach Loop

<html>
  <head>
    <title>Page Data Example</title>
  </head>
  <body>
    <table border="1">
      <form method="POST" action="params2.jsp">
        <tr>
          <td width="33%">
            <b>First Name</b>
          </td>
          <td width="73%">
            <input type="text" name="first" size="40" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>Last Name</b>
          </td>
          <td width="73%">
            <input type="text" name="last" size="40" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>Address</b>
          </td>
          <td width="73%">
            <input type="text" name="address" size="40" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>City</b>
          </td>
          <td width="73%">
            <input type="text" name="city" size="20" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>State</b>
          </td>
          <td width="73%">
            <input type="text" name="state" size="20" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>ZIP</b>
          </td>
          <td width="73%">
            <input type="text" name="zip" size="20" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" value="Submit" name="action" />
            <input type="reset" value="Reset" name="action" />
          </td>
        </tr>
      </form>
    </table>
  </body>
</html>
/////////////////////////////////////////////////////////////////////////////////////
//File: params2.zip





JSTL: fortokens

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





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