JSP 标准标签库(JSTL)中C标签的用法

c:forEach varStatus属性


current当前这次迭代的(集合中的)项
index当前这次迭代从 0 开始的迭代索引
count当前这次迭代从 1 开始的迭代计数
first用来表明当前这轮迭代是否为第一次迭代的标志
last用来表明当前这轮迭代是否为最后一次迭代的标志
begin属性值
end属性值
step属性值 


我们常会用c标签来遍历需要的数据,为了方便使用,varStatus属性可以方便我们实现一些与行数相关的功能,如:奇数行、偶数行差异;最后一行特殊处理等等。先就varStatus属性常用参数总结下:


${status.index}      输出行号,从0开始。
${status.count}      输出行号,从1开始。
${status.current}   当前这次迭代的(集合中的)项
${status.first}  判断当前项是否为集合中的第一项,返回值为true或false
${status.last}   判断当前项是否为集合中的最后一项,返回值为true或false
begin、end、step分别表示:起始序号,结束序号,跳跃步伐。


如:<c:forEach begin='1' end='5' step='2' items='${list}' var='item'>

表示:操作list集合汇中1~5条数据,不是逐条循环,而是按每2个取值。即操作集合中的第1、3、5条数据。


一个分页表格的显示例子



<body>
<h2>欢迎你: ${sessionScope.user.username}</h2>
<h4>用户展示</h4>
<table cellspacing="0" cellspadding="0" border="1">
  <tr>
    <th>序号</th>
    <th>用户名</th>
    <th>状态</th>
    <th>角色</th>
    <th>修改状态</th>
    <th>修改角色权限</th>
    <th>修改角色密码</th>
  </tr>

  <c:forEach   var="u" items="${pages.list}" varStatus="status">
    <tr>
      <td>${status.count+(pages.curIndex-1)*10}</td>
      <td>${u.username}</td>

      <c:choose>
        <c:when test="${u.status == 1}">
          <c:set var="sta" value="正常"></c:set>
        </c:when>

        <c:when test="${u.status == 2}">
          <c:set var="sta" value="离职"></c:set>
        </c:when>
      </c:choose>
      <td>${sta}</td>

      <c:choose>
        <c:when test="${u.role == 1}">
          <c:set var="role" value="普通人员"></c:set>
        </c:when>

        <c:when test="${u.role == 2}">
          <c:set var="role" value="管理员"></c:set>
        </c:when>

        <c:when test="${u.role == 3}">
          <c:set var="role" value="超级管理员"></c:set>
        </c:when>
      </c:choose>

      <td>${role}</td>

      <td><a href="LoadUpdateUserServlet?id=${u.id}&operation=${"status"}">修改状态</a></td>
      <td><a href="LoadUpdateUserServlet?id=${u.id}&operation=${"role"}">修改权限</a></td>
      <td><a href="LoadUpdateUserServlet?id=${u.id}&operation=${"password"}">修改密码</a></td>
    </tr>
  </c:forEach>
</table>
<a href="ListByPageServlet?curIndex=${pages.curIndex==1?1:pages.curIndex-1}">上一页</a>

<c:forEach var="p" begin="1" end="${pages.totalPage}" step="1">
    <a href="ListByPageServlet?curIndex=${p}">${p}</a>
</c:forEach>
<a href="ListByPageServlet?curIndex=${pages.curIndex==pages.totalPage?pages.totalPage:pages.curIndex+1}">下一页</a>
</table>
</body>

使用JSTL需要导入相关包


 (部分jstl支持EL表达式)

    -----jsp中EL表达式:11个${}------
    1.pageScope
    2.requestScope
    3.sessonScope
    4.applicationScope
    5.Cookies
    ..........

猜你喜欢

转载自blog.csdn.net/one_hwx/article/details/81037882
今日推荐