JSTL與EL表達式


jstl           : http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm;

                介紹:http://docs.oracle.com/javaee/6/tutorial/doc/(http://docs.oracle.com/javaee/6/tutorial/doc/bnacj.html#bnaco)

                 api:http://tomcat.apache.org/taglibs/standard/apidocs/

struts2 tag: http://struts.apache.org/release/2.1.x/docs/tag-reference.html

 

 

javascript中小腳本遍歷list(之前沒有轉換成json格式)

View Code
<script type="text/javascript" charset="UTF-8">

var productinfo= [
    <%List<Map<String, Object>> list = (List<Map<String, Object>>) request
                    .getAttribute("ProductItem");
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    try {
                        out.print("{code:\"" + list.get(i).get("PRODUCTCODE")
                                + "\",name:\"" + list.get(i).get("PRODUCTNAME")
                                + "\"}");
                        if (i != list.size() - 1) {
                            out.print(",");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {

                    }
                }
            }%>
    ];

 jstl+EL遍歷list map array

View Code
List:
<c:forEach var="student" items="${sessionScope.list}" varStatus="status">

index:${status.index }<br>
ID:${student.id }<br> 
Name:${student.name }<br>
Country:${student.address.country }<br>
City:${student.address.city }<br>
Street:${student.address.street } <br>

</c:forEach>


Map:
<c:forEach var="entry" items="${sessionScope.map}" begin="0" end="4" step="1" varStatus="status" >

index:${status.index }<br>
ID:${entry.key }<br> 
Name:${entry.value.name }<br>
Country:${entry.value.address.country }<br>
City:${entry.value.address.city }<br>
Street:${entry.value.address.street }<br> 

</c:forEach>


Array:
<c:forEach var="color" items="${sessionScope.colors}">
${color}<br>
</c:forEach>

 jstl函數庫

View Code

函數

描述

fn:contains(string, substring)

如果參數string中包含參數substring,返回true

fn:containsignorecase(string, substring)

如果參數string中包含參數substring(忽略大小寫),返回true

fn:endswith(string, suffix)

如果參數 string 以參數suffix結尾,返回true

fn:escapexml(string)

將有特殊意義的xml (和html)轉換為對應的xml character entity code,並返回

fn:indexof(string, substring)

返回參數substring在參數string中第一次出現的位置

fn:join(array, separator)

將一個給定的數組array用給定的間隔符separator串在一起,組成一個新的字符串並返回。

fn:length(item)

返回參數item中包含元素的數量。參數item類型是數組、collection或者string。如果是string類型,返回值是string中的字符數。

fn:replace(string, before, after)

返回一個string對象。用參數after字符串替換參數string中所有出現參數before字符串的地方,並返回替換后的結果

fn:split(string, separator)

返回一個數組,以參數separator 為分割符分割參數string,分割后的每一部分就是數組的一個元素

fn:startswith(string, prefix)

如果參數string以參數prefix開頭,返回true

fn:substring(string, begin, end)

返回參數string部分字符串, 從參數begin開始到參數end位置,包括end位置的字符

fn:substringafter(string, substring)

返回參數substring在參數string中后面的那一部分字符串

fn:substringbefore(string, substring)

返回參數substring在參數string中前面的那一部分字符串

fn:tolowercase(string)

將參數string所有的字符變為小寫,並將其返回

fn:touppercase(string)

將參數string所有的字符變為大寫,並將其返回

fn:trim(string)

去除參數string 首尾的空格,並將其返回

           //給遍歷賦值,相當於 <% int example = 100+1; %>

<c:set var= "example" value="${100+1}" scope="session"  />

<s:set name="personName" value="person.name"/>

 

          //打印變量,相當於 <%=example%>

<c:out value="${example}" default="a default value"  escapeXml="true"/>

<s:property value="#personName" default="a default value" />  OGNL的#符號相當與EL的$符號

 

         //從session中移除變量 <% session.removeAttribute("example")%>

<c:remove var= "example" scope="session"/>

 

         //test里面是一個boolean的運算, var里面聲明的變量則記錄test的結果,scope指明這個變量的存在范圍,有4個值,博文的第三點(JSP- 內置對象)

<c:if test= "${ 條件運算 }"  var= "varName" scope= "page"  />
        //條件為true時,執行的代碼
</c:if>

<s:if test="%{false}"><div>Will Not Be Executed</div>

</s:if>

<s:elseif test="%{true}"><div>Will Be Executed</div>

</s:elseif>

<s:else> <div>Will Not Be Executed</div>

</s:else>

 

        //items中是遍歷的集合變量,var里面聲明集合中的單個對象名稱為currentBook

<c:forEach var="currentBook" items="${sessionScope.titles}">
                …//顯示書籍信息
</c:forEach>

<s:iterator value="defaultDimensionItemList" status="st" id="infn">

</s>

 

 

 

 

一、循環遍歷集合
1、在jsp中引入標准函數聲明
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 


2、若要判斷集合的大小,則需要引入如下聲明
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


3、如何使用jstl判斷集合是否為空
${user}為集合,user為集合名
<c:if test="${empty user}">無信息!</c:if>為空
<c:if test="${!empty user}">其它</c:if>非空

json集合判斷是否為空:

 <c:choose>
    <c:when test="${'[null]' == coverageinfo}">//調試了好久,之前就寫的${ null == coverageinfo} or ${empty coverageinfo}
       $(function(){
           var coverageinfo= [];
       });
    </c:when>
    <c:otherwise>
        $(function(){
          var coverageinfo= ${coverageinfo};
        });
    </c:otherwise>
    </c:choose>

 


4、如何取得集合的大小
${fn:length(sessionScope.鍵)}
${fn:length(map)}

取得json集合的大小:${covArr}.length;

取得字符串的長度 :${fn:length("hello")}

 


5、varStatus顯示循環變量的狀態
例:<c:forEach var="currentFood" items="${sessionScope.foods}" varStatus="status"
<c:if test="${status%2==1}">
.....................
</c:if>
</c:forEach>
其中status這個變量就記錄了循環變量的狀態

 


6、如何遍歷Map集合
<c:forEach var="foodmap" items="${sessionScope.cart}" varStatus="status">
<c:set var="subtotal" value="${foodmap.value.bean.foodPrice*foodmap.value.quantity}"></c:set>
</c:forEach>
遍歷Map集合的值:
foodmap:保存session中的map
foodmap.value:取得map的值,即獲取保存在map中的一個對象
要獲取對象中的數據,必須用foodmap.value.quantity去點對象的屬性(quantity就是對象的屬性)

 

 

7.遍歷數字
  <c:if test="${totalPages>=1}">
      <a href="javascript:void(0)"  onclick="goback()">上一頁</a>
       <c:forEach begin="1" end="${totalPages}" var="i">
         <c:choose>
              <c:when test="${i != num}">
                 <a href="${path}/PageServlet?page=${i}&branchlevel=${branchlevel}&branchtype=${branchtype}">${i}</a>
              </c:when>
              <c:otherwise>
                 <a style="background-color: red" href="${path}/PageServlet?page=${i}&branchlevel=${branchlevel}&branchtype=${branchtype}">${i}</a>
              </c:otherwise>
          </c:choose>
       </c:forEach>
      <a href="javascript:void(0)" onclick="nextpage()">下一頁</a>
   </c:if>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM