Java/JSTL/Collections

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

JSTL Iteration over tokens

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Updatable Collections</title>
  </head>
  <body>
    <table border="0">
      <form method="post">
        <tr bgcolor="blue">
          <td>
            <p align="center">
              <b>
                <font color="#FFFFFF">Parse for Tokens</font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td valign="top">
            <p align="left">Enter a sentence:
            <br />
            <input width="20" maxwidth="20" name="text"
            size="50" />
            <br />
            &#160;</p>
          </td>
        </tr>
        <tr>
          <td valign="top">
            <p align="center">
              <input type="submit" name="parse" value="Parse" />
            </p>
          </td>
        </tr>
      </form>
    </table>
    <c:if test="${pageContext.request.method=="POST"}">
      <table border="1">
        <c:set var="i" value="1" />
        <c:forTokens items="${param.text}" var="word"
        delims=" ,.?!">
          <c:set var="i" value="${i}" />
          <tr>
            <td>
              <b>Word 
              <c:out value="${i}" />
              </b>
            </td>
            <td>
              <c:out value="${word}" />
            </td>
          </tr>
        </c:forTokens>
      </table>
    </c:if>
  </body>
</html>





JSTL Modify a collection

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<c:if test="${pageContext.request.method=="POST"}">
  <c:choose>
    <c:when test="${param.add!=null}">
      <c:if test="${list!=null}">
        <c:set var="list" value="${list}," scope="session" />
      </c:if>
      <c:set var="list" value="${list}${param.item}"
      scope="session" />
    </c:when>
    <c:when test="${param.remove!=null}">
      <c:set var="list2" value="" />
      <c:forEach var="item" items="${list}">
        <c:if test="${item!=param.item}">
          <c:if test="${list2!=""}">
            <c:set var="list2" value="${list2}," />
          </c:if>
          <c:set var="list2" value="${list2}${item}" />
        </c:if>
      </c:forEach>
      <c:set var="list" value="${list2}" scope="session" />
      <c:remove var="list2" />
    </c:when>
  </c:choose>
</c:if>
<html>
  <head>
    <title>Updatable Collections</title>
  </head>
  <body>
    <table border="0">
      <form method="post">
        <tr bgcolor="blue">
          <td colspan="2">
            <font color="white">Updatable Collections</font>
          </td>
        </tr>
        <tr>
          <td valign="top">
            <select NAME="choice" SIZE="5" width="20">
              <c:forEach var="item" items="${list}">
                <option>
                  <c:out value="${item}" />
                </option>
              </c:forEach>
            </select>
          </td>
          <td valign="top">Enter a item to add or remove.
          <br />
          <input width="20" maxwidth="20" name="item" size="20" />
          <br />
          <input type="submit" name="add" value="Add" />
          <input type="submit" name="remove" value="Remove" />
          </td>
        </tr>
      </form>
    </table>
  </body>
</html>





String Collection Examples

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>String Collection Examples</title>
  </head>
  <body>
    <h3>String Collection Example</h3>
    <c:set var="str"
    value="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" />
    <b>Input String:</b>
    <br />
    <br />
    <c:out value="${str}" />
    <br />
    <br />
    <b>Iterating:</b>
    <br />
    <br />
    <c:forEach var="day" items="${str}">
      <c:out value="${day}" />
      <br />
    </c:forEach>
  </body>
</html>