復習整理1:jsp標準標簽庫jstl
阿新 • • 發佈:2018-07-14
simple web-inf support bool http 核心 java @override 整理
一:是什麽:
JSTL是apache對EL表達式的擴展,JSTL是標簽語言!JSTL標簽使用以來非常方便,它與JSP動作標簽一樣,只不過它不是JSP內置的標簽,需要我們自己導包,以及指定標簽庫而已!
二:為什麽?有什麽作用?
jsp主要用於顯示業務邏輯代碼處理以後的數據結果,不可避免的使用循環,布爾邏輯,數據格式轉換等語句,使用jstl可以簡化代碼,便於管理,並且jstl可自定義標簽,更加便捷
三:怎麽用?
1: 導包
2:在jsp頁面頭部導入標簽庫。
1 <!--核心標簽--> 2 <%@ taglib prefix="c" 3 uri="http://java.sun.com/jsp/jstl/core" %> 4 <!--格式化標簽--> 5 <%@ taglib prefix="fmt" 6 uri="http://java.sun.com/jsp/jstl/fmt" %> 7 <!--SQL標簽--> 8 <%@ taglib prefix="sql" 9 uri="http://java.sun.com/jsp/jstl/sql" %> 10 <!--XML標簽--> 11 <%@ taglib prefix="x" 12uri="http://java.sun.com/jsp/jstl/xml" %>
13<!--jstl函數-->
14<%@ taglib prefix="fn"
15 uri="http://java.sun.com/jsp/jstl/functions" %>
3:使用標簽
1 <!--在pageContext中添加name為score value為${param.score }的數據--> 2 <c:set var="score" value="${param.score }"/> 3 <!--choose標簽對應Java中的if/else if/else結構。when標簽的test為true時,會執行這個when的內容。當所有when標簽的test都為false時,會執行otherwise標簽的內容。--> 4 <c:choose> 5 <c:when test="${score > 100 || score < 0}">錯誤的分數:${score }</c:when> 6 <c:when test="${score >= 90 }">A級</c:when> 7 <c:when test="${score >= 80 }">B級</c:when> 8 <c:when test="${score >= 70 }">C級</c:when> 9 <c:when test="${score >= 60 }">D級</c:when> 10 <c:otherwise>E級</c:otherwise> 11 </c:choose>
4:其他標簽網上都有,很多,不在多說,應熟練掌握練習
四:靈活用---自定義標簽
1: 新建標簽處理類,繼承自SimpleTagSupport類重寫doTag()方法。
1 public class IfTag extends SimpleTagSupport { 2 //test為iftag標簽的一個布爾類型的屬性 3 private boolean test; 4 //提供相關方法 5 public boolean isTest() { 6 return test; 7 } 8 public void setTest(boolean test) { 9 this.test = test; 10 } 11 @Override 12 public void doTag() throws JspException, IOException { 13 if(test) { 14 this.getJspBody().invoke(null); 15 } 16 } 17 }
2:在web項目的WEB-INF目錄的lib目錄下建立tld文件,這個tld文件為標簽庫的聲明文件,並配置好相關信息。
1 <tag> 2 <!--自定義的標簽名字--> 3 <name>if</name> 4 <!--類名--> 5 <tag-class>cn.xxx.IfTag</tag-class> 6 7 <body-content>scriptless</body-content> 8 9 <attribute> 10 <!--屬性名--> 11 <name>test</name> 12 <!--是否必選--> 13 <required>true</required> 14 <!--是否支持EL表達式--> 15 <rtexprvalue>true</rtexprvalue> 16 </attribute> 17 </tag>
3:引用自定義標簽
1 <% 2 pageContext.setAttribute("one", true); 3 pageContext.setAttribute("two", false); 4 %> 5 <it:if test="${one }">xixi</it:if> 6 <it:if test="${two }">haha</it:if> 7 <it:if test="true">hehe</it:if>
11
復習整理1:jsp標準標簽庫jstl