Java Tutorial/JSTL/ForTokens

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

JSTL ForTokens

<%@ 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><td>Parse for Tokens</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>





Use JSTL ForTokens to split string

<%@ taglib prefix="c"    uri="http://java.sun.ru/jstl/core" %>
<c:set var="names" value="A:B;C|D" 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>