java基礎開發—jstl標簽庫
在DRP項目中。接觸到了JSTL標簽庫。
在未使用Jstl之前,我們使用JSP腳本實現一些聲明或是表達式任務,做一些業務相關操作時,須要在頁面中嵌入大量的java代碼。在DRP項目開發前期。使用jsp頁面給我的感受就是。jsp頁面什麽活都能幹,權利過高。導致的結果就是,jsp頁面提高了程序的復雜性、增強了代碼維護的復雜度。以及代碼閱讀的困難。對程序猿的要求也相對較高。比如,雖然精通頁面設計的開發者來維護jsp。也經常須要依賴於經驗豐富的程序猿。所以須要一種技術來解決這些問題,以下簡介一下jstl:
Jstl(JSP Standard Tag Library)是由SUN公司提出的JSP的一個標準標簽庫。將經常使用功能包裝到定制標記庫集合中。
這些標記庫跟HTML標簽不同。它主要應用與server端。
JSTL包含五類標準標記庫:核心庫、格式庫、XML庫、SQL庫、函數庫。同一時候Jstl是與EL表達式一同使用的,E L(Expression Language) 目的:為了使JSP寫起來更加簡單。
個人覺得其並不屬於jstl,而是jstl使用了el表達式,一起更方便的服務於jsp。
EL表達式
語法:$和{}
內置對象: pageScope,requestScope,sessionScope,applicationScope
Empty操作符:決定對象、集合或字符串變量是否為空或null,比如:
servlet中定義:request.setAttribute("v2", "");
jsp:v1:${empty v1}<br>
“.“和“[ ]“兩種運算符來存取數據:比如:
${user.username}
${users[5].username }
運算符:【==/eq !=/ne </lt >/gt &&/and ||/or !/not //div %/mod】 比如:
<li>el表達式對運算符的支持</li><br> 1+1=${1+1 }<br> 10/5=${10/5 }<br> 10 div 5=${10 div 5 }<br> 10 % 3=${10 % 3 }<br> 10 mod 3 = ${ 10 mod 3 }<br>
Jstl標簽庫:
在使用jstl前提須要引入jstl.jar和standard.jar兩個jar包。
1.核心庫:引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="c"%>
c:out標簽:hello(使用標簽):<c:out value="${hello}"/><br>
c:set標簽:<c:set value="root" var="userid"/>
c:remove標簽:<c:remove var="userid"/>
條件控制標簽:c:if、c:choose、c:when
<c:choose> <c:when test="${v1 gt v2}"> v1大於v2<br> </c:when> <c:otherwise> v1小於v2<br> </c:otherwise> </c:choose> <c:choose> <c:when test="${empty userList}"> 沒有符合條件的數據<br> </c:when> <c:otherwise> 存在用戶數據<br> </c:otherwise> </c:choose>
循環控制標簽
<c:choose> <c:when test="${empty users}"> <tr> <td colspan="3">沒有符合條件的數據</td> </tr> </c:when> <c:otherwise> <c:forEach items="${users}" var="user"> <tr> <td>${user.username }</td> <td>${user.age }</td> <td>${user.group.name }</td> </tr> </c:forEach> </c:otherwise> </c:choose>
2.函數庫:引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<body> hello.length(使用JSTL的length函數, 函數必須放到EL表達式中,使用方法:前綴+冒號+函數名稱):${fn:length(hello) }<br> <p> <c:forEach items="${fn:split(str, \"#\") }" var="v"> ${v }<br> </c:forEach> <p> 自己定義函數:${my:say("張三") }<br> </body>
3.格式化庫:引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fm"%>
以下是獲取時間和日期的三種形式
today(type="date"):<fmt:formatDate value="${today}" type="date"/><br> today(type="time"):<fmt:formatDate value="${today}" type="time"/><br> today(type="both"):<fmt:formatDate value="${today}" type="both"/><br>
DRP項目中遍歷下拉框實例:
1.未使用Jstl
<tr> <td height="26"> <div align="right"> <font color="#FF0000">*</font>類別: </div> </td> <td> <% List<ItemCategory> itemCategoryList = (List)request.getAttribute("itemCategoryList"); %> <select name="category" class="select1" id="category"> <% for(Iterator<ItemCategory> iter = itemCategoryList.iterator();iter.hasNext();){ ItemCategory ic = iter.next(); %> <option value="<%=ic.getId()%>"><%=ic.getName()%></option> <% } %> </select> </td> </tr>
2.使用jstl
<tr> <td height="26"> <div align="right"> <font color="#FF0000">*</font>類別: </div> </td> <td> <select name="category" class="select1" id="category"> <c:forEach items="${itemCategoryList }" var="itemCategory"> <option value="${itemCategory.id }">${itemCategory.name }</option> </c:forEach> </select> </td> </tr>
總結:
對於程序猿來說。不將就是發現問題的動力源泉。有什麽樣的需求。就會創造出什麽樣的技術和工具。Jstl封裝了web應用程序的一些通用功能。我們在使用的時候,不必須要理解其內部是怎樣實現的,僅僅須要了解其標簽每一個標簽代表的功能就可以。
jstl的出現削弱jsp頁面過高的權利。使jsp頁面分層更加清楚而出現的。
java基礎開發—jstl標簽庫