JSP标签 jstl 的常见用法

使用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>&nbsp;
<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 是从零开始的

<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=\'#\'" /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>

注意:c:forEach 的begin 不是从1 开始的

(6)获取ArrayList的第一个元素

 <div class="slider-item"><img src=\'#\'" /></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>

注意:为什么status.count-1 呢?因为status.count 是从1开始的,而数组下标是从零开始的.


参考:http://hw1287789687.iteye.com/blog/2124369


你可能感兴趣的:(jstl,jsp标签,fn获取长度,fn截取字符串)