1.什麼是JSTL
apache對EL表達式的擴展(也就是說JSTL依賴EL)
JSTL是標籤語言!
2.JSTL標籤庫
一共有四大標籤庫
core:核心標籤庫,學習的重點
fmt:格式化標籤庫
sql:數據庫標籤庫,已經過時
xml:xml標籤庫,已經過時
3.使用taglib指令導入標籤庫
除了JSP動作標籤外,使用其他第三方的標籤庫都需要導包
在使用標籤的JSP頁面中使用taglib指令導入標籤庫
下面是導入JSTL的core標籤庫
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
prefix=”c”:指定標籤庫的前綴,這個前綴可以隨便給值,但大家都約定俗成使用c
uri=”xx”:指定標籤庫的uri,它不一定是真實存在的網址,但他可以讓JSP找到描述他的文件
core庫的c標籤
- out和set
<c:out value="${a}" default="xxx" escapeXml="false"/>
-----------
<c:set var="a" value="hello" scope="request">
相當於設置request.setAttribute(”)
默認值爲page,可選有page,request,session,application
remove
<c:remove var="a" scope="page"/>
如果不指定域,那就是刪除所有域中爲a的數據url
value:指定一個路徑,會自動添加項目名
var:本來要輸出的,和scope一起保存url
<c:url value="/index.jsp"/>
子標籤:用來給Url後面添加參數
<c:url value="/index.jsp">
<c:param name="username" value="章"/>
可以對參數進行url編碼
例子
<a href="<c:url value="/jsp/b.jsp">
<c:param name="d" value="陳"/>
</c:url>">點擊到b.jsp</a>
邏輯標籤
if
if標籤的test屬性必須是一個boolean類型的值,如果test的值爲true,那麼執行if標籤的內容,否則不執行
<c:set var="a" value="hello"/>
<c:if test="${not empty a}">
<c:out vlaue="${a}"/>
</c:if>
- choose 對應java中的if else中的else
<c:set var="score" value="${param.score}"/>
<c:choose>
<c:when test="${score>100||scope<0">錯誤的分數</c:when>
<c:when test="${score>=90"}>A級</c:when>
<c:when test="${score>=80"}>B級別</c:when>
<c:otherwise>...</c:otherwise>
</c:choose>
例子
訪問頁面時候攜帶參數?name=chenchenchen即可
<c:if test="${empty param.name}">
<c:out value="空"/>
</c:if>
....
<c:choose>
<c:when test="${not empty param.name}">
<c:out value="空"/>
</c:when>
<c:otherwise>
<c:out value="不空"/>
forEach
它用來循環遍歷數組、集合
她還可以用來計數方式來循環
<c:forEach var="i" begin="1" end="10">
${i}</br>
</c:forEach>
當然還可以調步長
屬性:
var表示循環變量
begin:設置循環變量從幾開始
end:設置循環變量到幾結束
step:設置步長 step=”3”
items 表示遍歷誰
<c:forEach items="${strs}" var="str">
${str}<br/>
</c:forEach>
forEach中的循環狀態
可以使用一個varstatus來創建循環狀態變量!
count:個數
index:下標
first:是否爲第一個元素
last:是否爲最後一個元素
current:當前元素
<%
Map map=new HashMap();
map.put("x","coco");
map.put("y","caoxi");
map.put("z","cyl");
request.setAttribute("map", map);
%>
<c:forEach items="${map}" var="e" varStatus="s">
s.count:${s.count}
s.index:${s.index}
first:${s.first}<br>
</c:forEach>
fmt標籤
時間標籤
<fm:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
Number標籤
<fm:formatNumber value="3333.3346" pattern="0.00000"></fm:formatNumber>不足會補充0
<fm:formatNumber value="3333.3346" pattern="#.#####"></fm:formatNumber>不會補充
4.自定義標籤
無標籤體
1.步驟
標籤處理類,標籤也是一個對象,也需要類
tld文件,他是一個xml
頁面中使用<%@taglib%>來指定tld文件的位置2.標籤處理類
SimpleTag接口
void doTag():每次執行標籤都會調用這個方法
JspTag getParent() 返回父標籤
void setParent(JspTag) 設置父標籤
void setJspBody(JspFragment) 設置標籤體
void setJspContext(JspContext) 設置JSP上下文對象,它兒子是pageContext例子
類
public class MyTag implements SimpleTag {
private PageContext pageContext;
private JspFragment body;
@Override
public void doTag() throws JspException, IOException {
pageContext.getOut().print("這是陳陳的標籤");
}
@Override
public JspTag getParent() {
return null;
}
@Override
public void setJspBody(JspFragment jspbody) {
this.body=jspbody;
}
@Override
public void setJspContext(JspContext jspcontext) {
this.pageContext=(PageContext)jspcontext;
}
@Override
public void setParent(JspTag arg0) {
}
}
zidingyi.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>coco</short-name>
<uri>http://chenyalan/2017/tld"</uri>
<tag>
<name>myT</name>
<tag-class>com.cn.jstl.MyTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
jsp
引入
<%@ taglib prefix="c" uri="/WEB-INF/tlds/zidingyi.tld"%>
<c:myT/>
輸出:這是陳陳的標籤
還有另外一個繼承類
public class MyTag2 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
this.getJspContext().getOut().print("接口");
}
}
有標籤體
標籤體內容
empty:無標籤體
JSP:jsp2.0已經不再支持這個類型了
scriptless:只能是el表達式,也可以是其他的標籤
tagdependent:標籤體內容不會被執行,而是直接被賦值給標籤處理類MyTag3.java
public class MyTag3 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
Writer out=this.getJspContext().getOut();//獲取當前Jsp頁面的輸出流
out.write("*********************************<br>");
this.getJspBody().invoke(out);
out.write("<br>*********************************<br>");
}
}
繼續在tld添加
<tag>
<name>myT3</name>
<tag-class>com.cn.jstl.MyTag3</tag-class>
<body-content>scriptless</body-content>
</tag>
jsp
<%
request.setAttribute("xxx","zzz");
%>
<c:myT3>
${xxx}
</c:myT3>
不執行標籤下面的標籤
如果想在執行了自定義標籤後,不再執行JSP頁面下面的東西,那麼就需要在doTag()方法中使用SkipPageException
throw new SkipPageException();
自定義標籤之有屬性的標籤
步驟
1.給你的標籤處理類添加屬性
2.在tld文件中對屬性進行配置類
public class MyTag4 extends SimpleTagSupport{
private boolean test;
public void setTest(boolean test){
this.test=test;
}
@Override
public void doTag() throws JspException, IOException {
if(test){
this.getJspBody().invoke(null); //如果傳遞的輸出爲null,表示使用的就是當前的out
}
}
}
繼續在tld裏添加
<tag>
<name>myT4</name>
<tag-class>com.cn.jstl.MyTag4</tag-class>
<body-content>scriptless</body-content>
<!-- 開始部署屬性 -->
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
jsp中使用
<c:myT4 test="${empty param.xxx}">
<br>dddddd
</c:myT4>
結果
xxxdds 接口
dddddd