在前面的開發(fā)中我們會發(fā)現(xiàn)經(jīng)常無法避免在jsp中或多或少的出現(xiàn)各種JSP腳本片段,那么導(dǎo)致頁面美工人員無法對其很好的維護。因此今天的技術(shù)可以幫助開發(fā)人員快速減少JSP中腳本的出現(xiàn)。
JSTL簡介JSTL即Jsp Standard Tag Libraries即Jsp的標(biāo)準(zhǔn)標(biāo)簽庫。該技術(shù)提供了很多的標(biāo)簽用于封裝JSP中常用的一些基本的業(yè)務(wù)邏輯。
主要的分類如下:
核心庫:主要封裝的是一些基本的核心的業(yè)務(wù)邏輯。
<%@taglib uri="http://java.sun.com/jsp/jstl/core" 格式化和國際化庫:主要封裝的是一些進行數(shù)據(jù)格式化和國際化的業(yè)務(wù)。如:日期格式化。 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f"%> xml庫:主要封裝的是一些解析XML數(shù)據(jù)的業(yè)務(wù)邏輯。 <%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="xml"%> SQL庫:主要封裝的是操作數(shù)據(jù)庫的業(yè)務(wù)邏輯。 <%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 函數(shù)庫:主演封裝的是常見函數(shù)。如:String <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fun"%> 1 JSTL引入 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 2 體驗 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <c:out value="<%= 23 == 23 %>"></c:out> 以上的標(biāo)簽可以將如下的輸出語句進行避免 <% out.println(“”) %> 1. c:out 2. c:set 舉例: <!-- map.put("QQ","123456789") --> <c:set target="<%= map %>" property="qq" value="123456789"></c:set> <%= map.get("qq") %> 3. c:remove 4. c:catch <c:catch var=""> à 處理異常,指定異常對象以什么屬性存儲在page域 </c:catch> 5. c:if 判斷語句 <c:if test=””> à 指定的條件語句 à 如果成立執(zhí)行標(biāo)簽體內(nèi)容 </c:if> 6. if…selse 7. c:forEach 循環(huán)(重點) 舉例: 舉例: 舉例: 8. c:url 舉例: <c:url var="index" value="http://www.itcast.cn" scope="page"> <c:param name="name" value="焦寧波"></c:param> </c:url> 9. c:redirect 重定向 10. c:forTokens 11. c:import標(biāo)簽 <c:out ? 輸出標(biāo)簽value="" ? 輸出的內(nèi)容,可以是輸出表達式<%= %>default="" ? 輸出的默認(rèn)值escapeXml=""> ? 是否以xml方式輸出數(shù)據(jù)</c:out>
舉例:<c:out value='<%=pageContext.getAttribute("name",PageContext.REQUEST_SCOPE)%>' escapeXml="false"></c:out><c:set ? 設(shè)置一個域或者集合中的屬性var="" ? 指定域中的屬性名value="" ? 指定域中的屬性值scope="" ? 指定域?qū)ο髏arget="" ? 指定集合名property=""> ? 集合的屬性名</c:set> 舉例:<c:set var="psw" value="root" scope="page"></c:set><br/> <c:out value='<%=pageContext.getAttribute("psw",PageContext.PAGE_SCOPE)%>'></c:out><c:remove ? 刪除域中的屬性var="" ? 指定屬性名scope=""/> ? 指定域名
舉例:<c:catch var="error"> <%= 12/0 %> </c:catch> 異常消息是: <c:out value='<%= ((Exception)pageContext.getAttribute("error",PageContext.PAGE_SCOPE)).getMessage() %>'></c:out>舉例:<c:if test='<%= pageContext.getAttribute("list",PageContext.session_SCOPE) == null %>'> <font color="red">數(shù)據(jù)為空!</font><br/> </c:if> <c:choose> ? 外部選擇 <c:when test=""> ? 指定的是條件語句 </c:when> <c:otherwise> ? 條件不成立 </c:otherwise> </c:choose>舉例:<c:choose> <c:when test='<%= "jack".equals("jack") && "root".equals("root2") %>'> <c:out value="歡迎"></c:out> </c:when> <c:otherwise> <c:out value="注冊"></c:out> </c:otherwise> </c:choose> <c:forEach ? 循環(huán)begin="" ? 循環(huán)的開始值end="" ? 循環(huán)結(jié)束的值step="" ? 循環(huán)的步長var="" ? 將循環(huán)出來的數(shù)據(jù)已指定的屬性名放置在page域items="" ? 循環(huán)的集合數(shù)據(jù)varStatus=""> ? 循環(huán)的狀態(tài)對象 </c:forEach>舉例:<c:forEach begin="0" end="10" var="i"> <c:out value='<%= pageContext.getAttribute("i",PageContext.PAGE_SCOPE) %>'></c:out><br/> </c:forEach><% List<String> list = new ArrayList<String>(); list.add("aaaa"); list.add("bbbb"); list.add("cccc"); session.setAttribute("list",list); %> <!-- pageContext.setAttribute("str","aaaa",PageContext.PAGE_SCOPE) --> <c:forEach items='<%=pageContext.getAttribute("list",PageContext.SESSION_SCOPE) %>' var="str"> <c:out value='<%= pageContext.getAttribute("str",PageContext.PAGE_SCOPE)%>'></c:out><br/> </c:forEach><c:forEach items='<%=pageContext.getAttribute("list",PageContext.SESSION_SCOPE) %>' var="str" varStatus="status"> <c:choose> <c:when test="${status.count % 2 == 0}"> <tr bgcolor="red"> </c:when> <c:otherwise> <tr bgcolor="yellow"> </c:otherwise> </c:choose> <td> <c:out value='<%= pageContext.getAttribute("str",PageContext.PAGE_SCOPE)%>'></c:out> </td> </tr> </c:forEach><table align="center" border="1"> <!-- pageContext.setAttribute("str","aaaa",PageContext.PAGE_SCOPE) --> <c:forEach items='<%=pageContext.getAttribute("list",PageContext.SESSION_SCOPE) %>' var="str" varStatus="status"> <tr bgcolor='${ status.count % 2 == 0 ? "gray" : "pink" }'> <td> <c:out value='<%= pageContext.getAttribute("str",PageContext.PAGE_SCOPE)%>'></c:out> </td> </tr> </c:forEach> </table><c:url var="" ? 指定屬性的名字value="" ? 指定屬性的值scope="" ? 指定域context=""> ? 指定網(wǎng)站</c:url>
舉例:<c:redirect url="/list" context="/day09_example"></c:redirect>
<c:forTokens items="james,jack,lucy,jnb" delims="," step="1" var="name"> <c:out value='<%= pageContext.getAttribute("name",PageContext.PAGE_SCOPE) %>'></c:out><br/> </c:forTokens>引入頁面<c:import url="test.jsp"></c:import>
|
新聞熱點
疑難解答