Java jstl core tag fortokens

http‮w//:s‬ww.theitroad.com

The JSTL <c:forTokens> tag is used to iterate over a string that is delimited by a specified separator character, and perform some action for each token. This tag is part of the JSTL core tag library, and is typically used to generate dynamic HTML content based on a delimited string.

The basic syntax for the <c:forTokens> tag is as follows:

<c:forTokens items="string"
              delims="delimiters"
              var="variable">
    <!-- JSP code that references the variable -->
</c:forTokens>

The attributes of the <c:forTokens> tag are as follows:

  • items: The string to tokenize. This attribute is required.
  • delims: The delimiter characters to use when tokenizing the string. This attribute is required.
  • var: The name of the variable to store each token in. This attribute is required.

Here's an example that shows how to use the <c:forTokens> tag to tokenize a string containing comma-separated values and display each value in an unordered list:

<c:set var="myString" value="apple,banana,orange" />


<c:forTokens items="${myString}" delims="," var="fruit">
        - ${fruit}

    </c:forTokens>

In this example, the <c:set> tag is used to create a string containing the values "apple", "banana", and "orange", separated by commas. The <c:forTokens> tag is then used to tokenize this string and display each value in an unordered list.

The resulting output of this code will be:

- apple

    - banana

    - orange

Note that the <c:forTokens> tag can also be used to tokenize strings using multiple delimiter characters, such as spaces and commas. The <c:forTokens> tag also provides a number of other attributes for controlling the tokenization, such as begin, end, and step, which allow you to tokenize only a subset of the string.