1. 程式人生 > >JSP中常用的的EL表達式的匯總

JSP中常用的的EL表達式的匯總

字符串 quest function spa master user 內容 區別 pan

Jsp基礎知識

  • jsp的組成

    1. html靜態頁面(css、javascript)
    2. java代碼 <% %> (_jspService方法中)
    3. 內置對象 out request
    4. 表達式 <%= %>
    5. 聲明方法和成員變量 <%! %>
    6. 指令 %@page %@include
    7. 動作 jsp:include jsp:forward
    8. 註釋 <%-- --%>
  • 包含關系

    • 靜態包含 <%@include file="footer.jsp" %>
    • 動態包含 <jsp:includ e page="header.jsp">
  • 靜態包含和動態包含的區別

    原理是否生產java文件是否生可以有同名變量包含的時機
    動態包含 方法的調用 生成 可以 執行class文件
    靜態包含 內容嵌套 不生成 不可以 轉譯

EL表達式

  • jar的引用

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
  • 常用語法

    • for循環

      <c:forEach items="${recommendList}" var="item">
          ${item.desc}
      </c:forEach>
      <!--利用下標取出前四個元素 status-->
      <c:forEach items="${split}" var="subString" varStatus="status">
          <c:if test="${status.index < 4}">
              <li class=""><img src="${baseUrl}${subString}" alt="用戶配圖丟失">
          </c:if>
      </c:forEach>
      <!-- 取出123 -->
      <c:forEach var
      ="x" begin="1" end="${pageProductHot.extra.listPageCount}"> <a href="${baseUrl}product/jsp/indexForH5.jsp">${x}</a> </c:forEach>
    • if判斷

      <!--取出前五個元素 -->
      <c:forEach items="${pageProductHot.list}" var="item" varStatus="status">
      <c:if test="${(status.index) < 5}">
          ${item.name}
      </c:if>   
      </c:forEach>
      <!--判斷字符串是否為“” 判斷是否為null-->
      <c:if test="${not empty item.coverImgUrl || item.coverImgUrl eq null}">
      
      </c:if>
      <!--分割字符串 並且判斷字符串長度-->
      <c:set value="${fn:split(item.imgUrls, ‘,‘) }" var="split" />
          <c:if test="${fn:length(‘${split}‘) == 1}"
          <img src="${baseUrl}${split[0]}">
          
          <c:if test="${fn:length(item.textContentShort) > 100}">
          </c:if>
      </c:if>
    • split分割字符串

      <!--分割字符串 得到字符串數組-->
      <c:set value="${fn:split(item.imgUrls, ‘,‘) }" var="split" />
    • replace替換字符串中的內容

      <c:set var="newLine" value="\r\n" />
      ${fn:replace(product.loanRequire, newLine, "<br />")}

JSP中常用的的EL表達式的匯總