淺談JSTL中如何利用list.size()處理IP地址
作者:wurushuang
本文將簡單談談在JSTL中如何利用list.size()處理IP地址,以及截取時間戳、自動關閉模態窗口等等內容。JSTL(JSP Standard Tag Library ,JSP標準標簽庫)是一個不斷完善的開放源代碼的JSP標簽庫。JSTL只能運行在支持JSP1.2和Servlet2.3規范的容器上,如tomcat 4.x。在JSP 2.0中也是作為標準支持的。
得到list.size()
Java代碼
- ${fn:length(listComment)}
- ${fn:length(listComment)}
處理IP地址
Java代碼
- <c:forTokens var="ip" items="${comment.ip}" delims="." begin="0" end="2">${ip}.</c:forTokens>*
- <c:forTokens var="ip" items="${comment.ip}" delims="." begin="0" end="2">${ip}.</c:forTokens>*
JSTL標簽顯示指定長度字符串
Java代碼
- <c:set var="log.logTitle" value="做一個截取字符串長度的測試"
- <c:choose>
- <c:when test="${fn:length(log.logTitle) > 10}">
- <c:out value="${fn:substring(log.logTitle, 0, 10)}......" />
- </c:when>
- <c:otherwise>
- <c:out value="${log.logTitle}" />
- </c:otherwise>
- </c:choose>
- <c:set var="log.logTitle" value="做一個截取字符串長度的測試"
- <c:choose>
- <c:when test="${fn:length(log.logTitle) > 10}">
- <c:out value="${fn:substring(log.logTitle, 0, 10)}......" />
- </c:when>
- <c:otherwise>
- <c:out value="${log.logTitle}" />
- </c:otherwise>
- </c:choose>
截取時間戳
Java代碼
- ${fn:substring(comment.time,0,19)}
- ${fn:substring(comment.time,0,19)}
自動關閉模態窗口
Java代碼
- <c:if test="${success=='ok'}" >
- <script type="text/javascript">
- alert("評論發表成功");
- parent.parent.location.reload();
- </script>
- </c:if>
- <c:if test="${success=='ok'}" >
- <script type="text/javascript">
- alert("評論發表成功");
- parent.parent.location.reload();
- </script>
- </c:if>
JSTL中varStatus和 var 屬性一樣,varStatus用于創建限定了作用域的變量。不過,由varStuts屬性命名的變量并不存儲當前索引值或當前元素,而是賦予 javax.servlet.jsp.jstl.core.LoopTagStatus 類的實例。該類定義了一組特性,它們描述了迭代的當前狀態,下面列出了這些特性:
Java代碼
- 特性 Getter 描述
- current getCurrent() 當前這次迭代的(集合中的)項
- index getIndex() 當前這次迭代從 0 開始的迭代索引
- count getCount() 當前這次迭代從 1 開始的迭代計數
- first isFirst() 用來表明當前這輪迭代是否為***次迭代的標志
- last isLast() 用來表明當前這輪迭代是否為***一次迭代的標志
- begin getBegin() begin 屬性值
- end getEnd() end 屬性值
- step getStep() step 屬性值
- <c:forEach items="${listZonenews}" var="zonenews" varStatus="s">
- ${s.count}. ${zonenews.title }
- </c:forEach>
【編輯推薦】
責任編輯:彭凡
來源:
javaeye