March 2, 2011

JSTL 1.1 - <c:forTokens> example


Example 1:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table border="1">
   <c:forTokens var="element" items="t1;t2/t3;t4/t5;t6,t7,t8;t9" delims=";,/"  varStatus="c">
      <tr>
         <td>${c.count}</td>
         <td>${element}</td>
      </tr>
   </c:forTokens>
</table>

prints,

1t1
2t2
3t3
4t4
5t5
6t6
7t7
8t8
9t9

Example 2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="testString" scope="application" value="t1;t2/t3;t4/t5/t6,t7,t8;t9" />
<table border="1">
   <c:forTokens var="element" items="t1;t2/t3;t4/t5;t6,t7,t8;t9" delims=";,/"  varStatus="c" begin="1" end="7" step="2">
      <tr>
         <td>${c.count}</td>
         <td>${element}</td>
      </tr>
   </c:forTokens>
</table>

prints,



1t2
2t4
3t6
4t8

No comments:

Post a Comment