Usage of <c:forEach> in jstl

In the development of JSP, iteration is an operation that is often used. For example, display the results of the query row by row, etc. In the early days of JSP, Scriptlets were usually used to implement iterative output of Iterator or Enumeration objects. Iterating is now greatly simplified via JSTL's iteration tags.

         There are two iteration tags supported by JSTL, namely < c : forEach > and < c :forTokens>. Introduced here is the < c : forEach > tag.

         Simply put, the role of the < c : forEach > tag is to iterate and output the content inside the tag. It can either perform a fixed number of iterative outputs, or determine the number of iterations based on the number of objects in the collection.

         < c : forEach > tag, which needs to be used in conjunction with the el expression

         The syntax of the < c : forEach > tag is defined as follows.

                  < c : forEach  var="name of each variable" items="list to iterate" varStatus="state of each object"

                           begin="where the loop starts"     end ="where the loop ends" step="looping step>  loop to output something                   </ c : forEach > The          < c : forEach > tag has some of the following attributes: l var: The name of the iteration parameter. The names of variables that can be used in the iteration body to represent each iteration variable. Type is String. l items: The collection to be iterated over. The types it supports will be explained below. l varStatus: The name of the iteration variable, which is used to indicate the status of the iteration, and the information of the iteration itself can be accessed. 

                             











l begin: If items is specified, the iteration starts from items[begin]; if no items is specified, it starts from begin. Its type is integer.

l           end : If items are specified, the iteration ends at items[ end ]; if no items are specified, the iteration ends at end . Its type is also integer.

l step: The step size of the iteration. 

         The items attribute of the < c : forEach > tag supports all standard collection types provided by the Java platform. Additionally, you can use this operation to iterate over elements in arrays, including arrays of primitive types. The collection types it supports and the elements to iterate are as follows:

l java.util.Collection: The elements obtained by calling iterator(). 

l java.util.Map: the instance obtained through java.util.Map.Entry. 

l java.util.Iterator: Iterator element. 

l java.util.Enumeration: Enumeration elements. 

l Object instance array: array elements. 

l Array of basic type values: packed array elements. 

l String delimited by comma: substring after splitting. 

l          javax.servlet.jsp.jstl.sql.Result:SQL查询所获得的行。 

         不论是对整数还是对集合进行迭代,<c:forEach>的varStatus属性所起的作用相同。和var属性一样,varStatus用于创建限定了作用域的变量(改变量只在当前标签体内起作用)。不过,由varStatus属性命名的变量并不存储当前索引值或当前元素,而是赋予javax.servlet.jsp.jstl.core.LoopTagStatus类的实例。该类包含了一系列的特性,它们描述了迭代的当前状态,如下这些属性的含义如下所示:

l          current:当前这次迭代的(集合中的)项。 

l          index:当前这次迭代从0开始的迭代索引。 

l          count:当前这次迭代从1开始的迭代计数。 

l          first:用来表明当前这轮迭代是否为第一次迭代,该属性为boolean类型。 

l          last:用来表明当前这轮迭代是否为最后一次迭代,该属性为boolean类型。 

l          begin:begin属性的值。 

l          endend属性的值 

l          step:step属性的值 

下面就来看两个基本的例子,第一个例子是依次输出集合内的元素。 
<c:forEach var="item" items="${contents}" varStatus="status">
         $status.count:${item} 
</c:forEach
下面的例子是一个固定次数的迭代,用来输出1到9的平方。 
<c:forEach var="x" begin="1"end="9" step="1"> 
         ${x*x} 

</c:forEach>

下面写一下,我做的项目中用到的例子:

分页:

     <table>
       <tr><th>名字</th><th>说明</th><th>图片预览</th></tr>
        <c:forEach items="${data}" var="item">
            <tr><td>${item.advertName}</td><td>${item.notes}</td><td><img src="${item.defPath}"/></td></tr>
        </c:forEach>

     </table>
    <ul>
        <li><a href='?nowPage=${nowPage-1}'>←上一页</a></li>
                   <c:forEach varStatus="i" begin="1" end="${sumPage}">
                        <c:choose>
                           <c:when test="${nowPage==i.count}">
                              <li class='disabled'>${i.count}</li>
                           </c:when>
                           <c:otherwise>
                               <li  class='active'><a href='?nowPage=${i.count}'>${i.count}</a></li>
                           </c:otherwise>
                        </c:choose>
                   </c:forEach>

            <li><a href='?nowPage=${nowPage+1}'>下一页→</a></li>
      </ul>

首页展示图片的例子:

迭代后台传过来的list, src的路径要写绝对路径,写成相对路径会报错,有时绝对路径用<c:ulr>来写

           <c:forEach items="${lists}" var="item">
             <img id="img${i}" height="250" width="500"  class="img" src='UploadImages/${item.advertPath}'/>
            </c:forEach>

获得下标,其中size是后台传过来的list的长度,此处不能写成end="${list.size}"

              <c:forEach begin="1" end="${size}"  step="1" varStatus="i">
                    <li> <a href="http://www.baidu.com/" class="showimg">${i.index}</a></li>
                 </c:forEach>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327069987&siteId=291194637