1. 程式人生 > >【jstl】jsp標準標籤庫——標籤使用方法整理

【jstl】jsp標準標籤庫——標籤使用方法整理

1、c:if (沒有c:else)

<c:if test="${age > 20}">age大於20</c:if> //age大於20時,返回true

<c:if test="${falg}">flag為true</c:if> //flag是一個布林值,如果不是,那麼頁面會有型別不匹配的報錯

<c:if test="${empty str}">str為空</c:if>//判空,相當於<s:if test="str==''"></s:if>

<c:if test="${not empty str}
"
>str不為空</c:if>//判空,相當於<s:if test="str!=''"></s:if>

2、c:forEach

<c:forEach items="${users}" var="user" varStatus="s">
    序號:${s.index+1}//索引從0開始
    使用者名稱:${user.name}
    年齡:${user.age}
</c:forEach>

items:後臺傳來的一個集合,這裡接受的集合,不支援陣列
var:相當於for(User user:users){}中的user,從集合中取出的臨時物件
varStatus:當前foreach的狀態,可以獲取當前迭代到了第幾個物件 如:${s.index}
即為獲取當前物件的索引