使用Java Web开发时,会经常使用jstl,它是一个轻量级的标签库,不像struts2 那样庞大.
现在总结jstl的常用场景
(1)如何在页面中引入jstl库
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
(2)截取字符串
- <td>${fn:substring(ordersDetail.toothOrders.inDate,0,19) }</td>
(3)判断用户是否已经登录
- <c:if test="${sessionScope.logined!=null && sessionScope.user.role_id=='1'}">
- <a href="<%=path%>/orders/export" class="daoc" onclick="return com.whuang.hsj.confirmDelete('确定要导出吗?')" >导 出(快)</a>
- <a href="<%=path%>/orders/exportSlow" class="daoc" onclick="return com.whuang.hsj.confirmDelete('确定要导出吗(包含产品详情)?')" >导 出(慢)</a>
- </c:if>
(4)判断变量
- <c:choose>
- <c:when test="${view.totalRecords==0}"><font color="#df625c">0</font> </c:when>
- <c:otherwise>
- ${view.totalRecords }
- </c:otherwise>
- </c:choose>
- <c:choose>
- <c:when test="${fn:length(ordersDetail.valid)==0 ||fn:length(fn:trim(ordersDetail.valid))==0 }">无</c:when>
- <c:otherwise>${ordersDetail.valid }</c:otherwise>
- </c:choose>
(5)c:forEach 的begin 是从零开始的
注意:c:forEach 的begin 不是从1 开始的
- <table class="frontproductTable" >
- <tr>
- <c:forEach begin="1" end="2" varStatus="status">
- <td class="frontproduct" >
- <table cellspacing="0" border="0">
- <c:forEach var="orders" begin="${(status.count-1)*5 }" end="${(status.count)*5-1 }" items="${view.recordList}"
- varStatus="status">
- <tr>
- <td rowspan="1">
- <a href="<%=path%>/index/product_detail?id=${orders.id }" >
- <img width="280px" alt=""
- src="<%=path%>${orders.picPath }" style="width:300px; height:300px; margin:0 20px 10px 10px; border:1px solid #ccc; float:left;">
- </a>
- </td>
- <td rowspan="1">
- <table >
- <tr>
- <td valign="top"><strong>产品名称:</strong></td>
- <td>${orders.itemname }</td>
- </tr>
- <tr>
- <td colspan="2" ><strong>产品详情:</strong></td>
- </tr>
- <tr>
- <td colspan="2" >${orders.desc }</td>
- </tr>
- </table>
- </td>
- </tr>
- </c:forEach>
- </table></td>
- </c:forEach>
- </tr>
- </table>
(6)获取ArrayList的第一个元素
- <div class="slider-item"><img src="<%=path%>${carouselDiagrams[0].picPath}"/></div>
- <c:forEach var="carouselDiagram" begin="1" items="${carouselDiagrams}" varStatus="status">
- <div class="slider-item"><img id="img_id_${status.count}" /> </div>
- </c:forEach>
(7)获取List的长度
- <ul id="slider_nav">
- <c:forEach begin="1" step="1" end="${fn:length(carouselDiagrams) }" >
- <li><a href="#"></a></li>
- </c:forEach>
- </ul>
注意:fn:length 在<%@ taglib prefix="fn" uri="http://Java.sun.com/jsp/jstl/functions"%>中.
(8)让c:forEach 按照顺序输出
- <c:forEach begin="1" step="1" end="${fn:length(list) }" varStatus="status" >
- <li>
- <label>${list[status.count-1].description }:</label>
- ${list[status.count-1].value }
- <label> </label>
- </li>
- </c:forEach>
(9)循环
- <c:forEach items="${commonDictionaries }" var="commonDictionary" varStatus="status" >
- <section >
- <a href="#" ><h3>${commonDictionary.value } <span style="display: none" >></span> </h3></a>
- </section>
- </c:forEach>
- <c:forEach varStatus="status" items="${view.recordList }"
- var="paperNews">
- <li style="margin-right: 10px"><!--<a href="#"><img src="${paperNews.pic }"
- alt="${paperNews.title }" class="alignleft" /></a>
- <p>
- <span>发表日期${paperNews.releaseTimeStr }</span> <a href="#">${paperNews.title }</a>
- </p> <span class="rating"><span style="width: 80%;"></span></span> -->
- <img src="${paperNews.pic }" alt="${paperNews.title }" class="alignleft aHref" onclick="tabSelect('<%=path%>/news/${paperNews.id }?1=1',this,false)" />
- <h6 class="regular"><a class="aHref" onclick="tabSelect('<%=path%>/news/${paperNews.id }?1=1',this,false)" >${paperNews.title }</a></h6>
- <span class="meta">${paperNews.releaseTimeStr } <!-- \\ <a href="#">World News.</a> \\ <a href="#">No Coments.</a> --></span>
- <p>${fn:substring(paperNews.content,0,60)}...</p>
- </li>
- </c:forEach>
时间: 2024-11-25 02:17:30