天天看點

JSP标簽 jstl 的常用方法

現在總結jstl的常用場景

(1)如何在頁面中引入jstl庫

JSP标簽 jstl 的常用方法

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>  

(2)截取字元串

JSP标簽 jstl 的常用方法

<td>${fn:substring(ordersdetail.toothorders.indate,0,19)   }</td>  

(3)判斷使用者是否已經登入

JSP标簽 jstl 的常用方法

 <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)判斷變量

JSP标簽 jstl 的常用方法

<c:choose>  

                    <c:when test="${view.totalrecords==0}"><font color="#df625c">0</font> </c:when>  

                    <c:otherwise>  

                        ${view.totalrecords }  

                    </c:otherwise>  

                </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>  

 (5)c:foreach 的begin 是從零開始的

注意:c:foreach 的begin 不是從1 開始的

JSP标簽 jstl 的常用方法

<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>  

                                    <table  >  

                                        <tr>  

                                            <td valign="top"><strong>産品名稱:</strong></td>  

                                            <td>${orders.itemname }</td>  

                                        </tr>  

                                            <td colspan="2" ><strong>産品詳情:</strong></td>  

                                            <td colspan="2" >${orders.desc }</td>  

                                    </table>  

                            </tr>  

                        </c:foreach>  

                    </table></td>  

</c:foreach>  

    </tr>  

</table>  

(6)擷取arraylist的第一個元素

JSP标簽 jstl 的常用方法

<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的長度

JSP标簽 jstl 的常用方法

<ul id="slider_nav">  

                         <c:foreach begin="1" step="1"  end="${fn:length(carouseldiagrams) }" >  

                           <li><a href="#"></a></li>  

                           </c:foreach>  

                         </ul>  

 (8)讓c:foreach 按照順序輸出

JSP标簽 jstl 的常用方法

<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)循環

JSP标簽 jstl 的常用方法

<c:foreach      items="${commondictionaries }" var="commondictionary"  varstatus="status" >  

                            <section  >  

                                <a href="#" ><h3>${commondictionary.value } <span style="display: none" >></span> </h3></a>  

                            </section>  

                            </c:foreach>   

JSP标簽 jstl 的常用方法

<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>