JSTL標籤之c:foreach,c:if標籤小結

來源:互聯網
上載者:User

標籤:預設值   out   show   發布   iterator   字元   names   tor   ***   

<c:forEach>標籤用於通用資料迴圈,它有以下屬性

屬 性 描 述 是否必須 預設值
items 進行迴圈的項目 否 無
begin 開始條件 否 0
end 結束條件 否 集合中的最後一個項目
step 步長 否 1
var 代表當前項目的變數名 否 無
varStatus 顯示迴圈狀態的變數 否 無

 

begin和end屬性

可以指定開始迴圈位置和結束迴圈位置。例如

從集合第二個元素開始迭代:

<c:forEach items="${userInfo.roleTypeEnNames}" var="item" varStatus="status" begin="1"></c:forEach>

從集合第一個元素迭代,第二個元素結束

<c:forEach items="${userInfo.roleTypeEnNames}" var="item" varStatus="status" begin="0" end="1"></c:forEach>

vaStatus 屬性

JSTL中的varStatus和 var 屬性一樣, varStatus 用於建立限定了範圍的變數。不過,由 varStatus 屬性命名的變數並不儲存當前索引值或當前元素,而是賦予 javax.servlet.jsp.jstl.core.LoopTagStatus 類的執行個體。該類定義了一組特性,它們描述了迭代的目前狀態,下面列出了這些特性:

特性         Getter                       描述current    getCurrent()           當前這次迭代的(集合中的)項index       getIndex()               當前這次迭代從 0 開始的迭代索引count       getCount()             當前這次迭代從 1 開始的迭代計數first          isFirst()                  用來表明當前這輪迭代是否為第一次迭代的標誌last          isLast()                  用來表明當前這輪迭代是否為最後一次迭代的標誌begin      getBegin()             begin 屬性值end         getEnd()                 end 屬性值step        getStep()                step 屬性值

用c:forEach 中vaStatus的屬性可以獲得迭代的自身狀態,如:
<c:forEach var=”" items=”" varStatus=”status”>
<c:out value=”${status.current}”/> 當前對象
<c:out value=”${status.index}”/> 此次迭代的索引
<c:out value=”${status.count}”/> 已經迭代的數量
<c:out value=”${status.first}”/> 是否是第一個迭代對象
<c:out value=”${status.last}”/> 是否是最後一個迭代對象
</c:forEach>

<c:forEach>標籤的items屬性支援Java平台所提供的所有標準集合類型。此外,您可以使用該操作來迭代數組(包括基本類型數組)中的元素。它所支援的集合類型以及迭代的元素如下所示:

java.util.Collection:調用iterator()來獲得的元素。

java.util.Map:通過java.util.Map.Entry所獲得的執行個體。

java.util.Iterator:迭代器元素。

java.util.Enumeration:枚舉元素。

Object執行個體數組:數組元素。

基本類型值數組:經過封裝的數組元素。

用逗號定界的String:分割後的子字串。

javax.servlet.jsp.jstl.sql.Result:SQL查詢所獲得的行。

*****************************************無敵分割線****************************************

<c:if>標籤有如下屬性:

屬性 描述 是否必要 預設值
test 條件 是 無
var 用於儲存條件結果的變數 否 無
scope var屬性的範圍 否 page

 

例子:判斷item是否包含ledcdistrictMng

<c:if test="${fn:contains(item,‘ledcdistrictMng‘)}">
<tsp:show value="zxjx.createAndRecommendCourse">
<div class="mlr25 schb-btns">
<a href="${ctx }
onlineteaching/course/courseStudy_courseStudyRecords.action?userId=${userInfo.userId}&flag=0"
class="begin"><span class="begin-ic">已學課程</span>

</a>
<a href="${ctx}onlineteaching/teaCreCourse_creCourseStepTwo.action?type=3" class="recom">

<span class="recom-ic">發布課程
</span>

</a>
</div>
</tsp:show>
</c:if>

 

由於只有<c:if>沒有<c:else>,所以要用c:else可以用下面方法代替:

<c:choose>
<c:when test="${fn:contains(item,‘ledcdistrictMng‘)}">

111111
</c:when>
<c:otherwise>

222222
</c:otherwise>

</c:choose>

 

JSTL標籤之c:foreach,c:if標籤小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.