EL表達式
在JSP頁面中可以直接使用
EL用來代替<%= %>輸出,EL表達式是用來輸出 的。
在頁面輸出的幾種方式
<%
request.setAttribute("name","request");
%>
方式一 out:<% out.print(request.getAttribute("name"));%>
方式二 表達式:<%=request.getAttribute("name") %>
方式三 EL表達式:
語法: ${表達式} ${name }
11個內置對象
取值範圍
四個域的尋找順序是pageContext、request、session、application。若是找不到鍵值爲指定屬性的屬性值,不會顯示null,會顯示空字符串。
${pageScope.name }
${requestScope.name }
${sessionScope.name }
${applicationScope.name }
取值List數組
// 集合
List<String> list = new ArrayList<String>();
list.add("劉備");
list.add("關羽");
list.add("張飛");
HttpSession session =request.getSession();
session.setAttribute("list", list);
--------------------------------------------
<!-- 集合 -->
${list[1] }
缺點:EL表達式不支持遍歷只能根據指定的下標獲取值。
取值Map集合
//Map
Map<String,User> map = new HashMap<>();
map.put("one", new User("c羅",33));
map.put("two", new User("梅西",31));
map.put("three", new User("內馬爾",26));
request.setAttribute("map", map);
---------------------------------------
<!-- Map 先獲取鍵值,再根據鍵值獲取屬性值 -->
${map.one.name }
支持運算符
1)語法${運算表達式 }
2)常見運算符
描述 | 格式 |
---|---|
== | eq |
!= | ne |
< | lt |
> | gt |
<= | le |
>= | ge |
&& | and |
|| |
or |
! | not |
判空 | empty |
三目運算符
${name==null?"null":name }
${aaa==bbb?1:0}
JSTL標籤
JSTL標籤是apache對EL表達式的擴展,是標籤語言。
不是JSP的內置標籤,使用時需要導包。
JSTL標籤庫包括五個
描述 | 解釋 |
---|---|
core:核心標籤庫 | 核心標籤,執行一些輸出,設置值,可遍歷 |
fmt:格式化標籤庫 | 格式化標籤,可格式化日期、數字 |
sql:數據庫標籤庫 | 用於在頁面執行sql |
xml:xml標籤庫 | 操作XML的標籤 |
JSTL函數 | 字符串處理函數 |
JSP頁面使用taglib指令導入標籤庫
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
prefix=“c”:指定標籤庫的前綴
uri=“http://java.sun.com/jsp/jstl/core”:指定標籤庫的uri
core標籤
c:out 輸出信息到頁面
<%
request.setAttribute("name", "張三");
%>
<c:out value="${name }" default="張某某"></c:out>
c:set設置值
`<!-- c:set 設置屬性 scope:可選四大域對象page、request、
session、application;默認page` -->
<c:set var="sex" value="男" scope="request"></c:set>
c:if判斷
<c:if test="${param.age<40 and param.age>=18}">
<span style="color:blue;">青年</span></c:if>
c:choose 判斷 相當於 if elseif else
<c:choose>
<c:when test="${param.age<18 }">
少年 <br>
</c:when>
<c:when test="${param.age<40 }">
青年 <br>
</c:when>
<c:otherwise>
中老年 <br>
</c:otherwise>
</c:choose>
c:forEach遍歷
// 數組
String[] arr = { "張三", "李四", "王五" };
request.setAttribute("arr", arr);
----------------------------------------
<!-- C:forEach items:遍歷的對象 var:每一次要遍歷的元素 -->
<c:forEach items="${arr }" var="str">
${str }
</c:forEach>
// 集合
List<String> list = new ArrayList<String>();
list.add("劉備");
list.add("關羽");
list.add("張飛");
HttpSession session =request.getSession();
session.setAttribute("list", list);
----------------------------------------
<c:forEach items="${list }" var="name">
${name }
</c:forEach>
//Map
Map<String,User> map = new HashMap<>();
map.put("one", new User("c羅",33));
map.put("two", new User("梅西",31));
map.put("three", new User("內馬爾",26));
request.setAttribute("map", map);
--------------------------------
<!-- 遍歷Map -->
<c:forEach items="${map }" var="entry">
${entry.key}:${entry.value.name}:${entry.value.age }
</c:forEach>
<!-- 遍歷0-10之間的數字 -->
<c:forEach var="i" begin="0" end="10" step="2">
${i }
</c:forEach>
<c:forEach items="${users }" var="user" varStatus="status">
${user.name }:${status.index }:${status.countl }
</c:forEach>