为什么需要“items”=标准标签c:forEach?

时间:2014-08-04 16:24:16

标签: java jsp jstl

所以这是来自c.tld,标签库Descriptior为JSTL标签开头

<tag>
    <description>
        The basic iteration tag, accepting many different
        collection types and supporting subsetting and other
        functionality
    </description>
    <name>forEach</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
    <tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
            Collection of items to iterate over.
        </description>
        <name>items</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.Object</type>
        <deferred-value>
            <type>java.lang.Object</type>
        </deferred-value>
    </attribute>

我的问题是,如何在没有&#34;项目的情况下使用forEach&#34; ?为什么需要= false?

这是如何与项目一起使用的:

<c:forEach items="${requestScope.songs}" var="song">
    Song: ${song}
    <br />
</c:forEach>

作为一个例子..

2 个答案:

答案 0 :(得分:3)

您可以使用forEach标记上的其他可用属性来构成一个简单的计数器,而不需要迭代的特定值数组,例如:

<p>The numbers from one to 10 are:</p>
<ul>
<c:forEach var="i" begin="1" end="10">
   <li><c:out value="${i}"/></li>
</c:forEach>
</ul>

对于step属性,这可能非常有用。

答案 1 :(得分:1)

您可以将它与项目一起使用..如下所示

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