JSP中嵌套了大量的Java代碼增加了頁面的複雜度,爲了簡化頁面,管理代碼,Apache組織開發了一套標籤庫(JSTL:JSP Standard Tag Library)
EL:簡單的計算規則。
EL表達式作用:
1、訪問Bean的屬性。${對象名.屬性名}或者${對象名["屬性名"]},執行過程:容器依次從pageContext,request,session,application中查找。可以指定查找域(sessionScope,pageScope,requestScope,applicationScope)例如:${sessionScope.user.name}
2、輸出簡單的運算結果,+-*/%,&&,||,!,>=,empty。例如:${empty str}
3、獲取請求參數值。${param.username} 獲取${paramvalues.city}相對於request.getParameter("username"),request.getParameterValues("city")
JavaBean:公共類,有無參構造方法,get ,set
JSTL使用步驟:
1、導入JSTL對應的jar包
2、使用taglib指令導入JSP標籤。例如:<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
常用的標籤:
<c:if test="${student.gender=='m'}" var="rs" scope="request">
男
</c:if>
<c:if test="${!rs}">女</c:if>
<c:choose>
<c:when test=""></c:when>
<c:otherwise></c:otherwise>.
</c:choose>
<c:forEach var="" items="" varStatus="s">
<p class="s${s.index%2+1}">
</c:forEach>
自定義標籤:
1、編寫Java類,繼承SimpleTagSupport,在doTag方法中添加處理邏輯。配置標籤說明文件
標籤運行原理:容器根據JSP標籤的命名空間找到標籤的描述文件(.tld文件),然後根據標籤名字找到標籤類,接下來將該標籤類實例化。