Jsp-jstl-core-foreach-tag

提供:Dev Guides
移動先:案内検索

JSTL-コア<c:forEach>、<c:forTokens>タグ

これらのタグは、スクリプトレットを介してJavaの* for、while、または *do-while ループを埋め込むための優れた代替手段として存在します。 <c:forEach> タグは、オブジェクトのコレクションを反復処理するため、一般的に使用されるタグです。 <c:forTokens> タグは、文字列をトークンに分割し、各トークンを反復処理するために使用されます。

属性

*<c:forEach>* タグには次の属性があります-
Attribute Description Required Default
items Information to loop over No None
begin Element to start with (0 = first item, 1 = second item, …​) No 0
end Element to end with (0 = first item, 1 = second item, …​) No Last element
step Process every step items No 1
var Name of the variable to expose the current item No None
varStatus Name of the variable to expose the loop status No None
*<c:forTokens>* タグには、区切り文字として使用するシャラクターを指定する1つの追加属性 *delims* を除いて、 *<c:forEach>* タグと同様の属性があります。
Attribute Description Required Default
delims Characters to use as delimiters Yes None

<c:forEach>の例

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
   <head>
      <title><c:forEach> Tag Example</title>
   </head>

   <body>
      <c:forEach var = "i" begin = "1" end = "5">
         Item <c:out value = "${i}"/><p>
      </c:forEach>
   </body>
</html>

上記のコードは、次の結果を生成します-

Item 1
Item 2
Item 3
Item 4
Item 5

<c:forTokens>の例

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
   <head>
      <title><c:forTokens> Tag Example</title>
   </head>

   <body>
      <c:forTokens items = "Zara,nuha,roshy" delims = "," var = "name">
         <c:out value = "${name}"/><p>
      </c:forTokens>
   </body>
</html>

上記のコードは、次の結果を生成します-

Zara
nuha
roshy