EL表達式
一、定義與作用
定義:
EL表達式的全稱:Expresssion Language,表達式語言。
作用:
主要替代jsp頁面中的表達式腳本在jsp頁面中進行資料的輸出。
替換原因:
EL表達式在輸出資料時,要比jsp表達式腳本要簡潔
<body>
<%
request.setAttribute("key","value");
%>
jsp表達式腳本輸出key的值是:<%=request.getAttribute("key1")==null?"":request.getAttribute("key1")%> <br>
EL表達式輸出key的值是:${key1}
</body>
文法:
${表達式}
EL表達式輸出null值時,輸出的是 空串。
jsp表達式腳本輸出null值時,輸出的是 null字元串。
二、EL表達式搜尋域資料的順序
EL表達式主要是在jsp頁面輸出資料。
主要輸出域對象中的資料。
<body>
<%
//往四個域中都儲存了相同的key
pageContext.setAttribute("key","pageContext");
request.setAttribute("key","request");
session.setAttribute("key","session");
application.setAttribute("key","application");
// pageContext< request< session< application
%>
${key}
</body>
當四個域都有相同的key時,EL表達式會按照四個域的從小到大的順序去進行搜尋,找到就輸出,代碼的先後順序無關。
三、EL表達式的輸出情況
Person 類
public class Person {
// i. 需求 —— 輸出 Person 類中普通屬性,數組屬性。 list 集合屬性和 map 集合屬性。
private int id;
private String name;
private String[] phones;
private List<String> cities;
private Map<String,Object> map;
public int getid() {
return 18;
}
輸出的代碼
<body>
<%
Person person = new Person();
person.setName("阿昌加油");
person.setPhone(new String[]{"110","120","119","114"});
ArrayList<String> cities = new ArrayList<>();
cities.add("溫州");
cities.add("杭州");
cities.add("嘉興");
cities.add("湖州");
cities.add("衢州");
person.setCities(cities);
HashMap<String, Object> map = new HashMap<>();
map.put("中國","北京");
map.put("日本","東京");
map.put("美國","華盛頓");
map.put("英國","倫敦");
person.setMap(map);
pageContext.setAttribute("person",person);
%>
輸出person:${person} <br>
輸出person的name屬性: ${person.name}<br>
輸出person的phone數組屬性值: ${person.phone[0]}<br>
輸出person的cities集合元素值: ${person.cities}<br>
輸出person的cities集合中第一個元素值: ${person.cities[0]}<br>
輸出person的map集合: ${person.map}<br>
輸出person的map集合中第三個key的值: ${person.map.美國}<br>
輸出person的id的值: ${person.id}<br>
</body>
四、EL表達式 —— 運算
文法:
${ 運算表達式 }
1) 關系運算
2)邏輯運算
3)算術運算
4)empty 運算
empty運算可以判斷一個資料是否為空,如果為空,則輸出true,反之為false。
以下幾種情況為空:
1、值為null時,為空
2、值為空串,為空
3、值是Object類型數組,長度為0時
4、ist集合,元素個數為0
5、map集合,元素個數為0
<body>
<%
// 1、值為null時,為空
request.setAttribute("emptyNull",null);
// 2、值為空串,為空
request.setAttribute("emptyStr","");
// 3、值是Object類型數組,長度為0時
request.setAttribute("emptyObj",new Object[0]);
// 4、List集合,元素個數為0
List<String> list = new ArrayList<>();
request.setAttribute("emptyList",list);
// 5、map集合,元素個數為0
HashMap<String, Integer> map = new HashMap<>();
request.setAttribute("emptyMap",map);
%>
${ empty emptyNull} <br>
${ empty emptyStr} <br>
${ empty emptyObj} <br>
${ empty emptyList} <br>
${ empty emptyMap} <br>
</body>
5) 三元運算
文法:
表達式1?表達式2:表達式3
如果表達式1為true,則傳回表達式2的值,反之傳回表達式3
執行個體
${ 12 == 12 ? "阿昌加油" : "阿昌沖沖沖" }
6)“ . ”點運算 和 和 [] 中括号運算符
作用
.點運算,可以輸出Bean對象中某個屬性的值。
[]中括号運算,可以輸出有序集合中某個元素的值。且還可輸出map集合中keyi含有特殊字元key的值
<body>
<%
Map<String,Object> map = new HashMap<>();
map.put("a.a.a","valueA");
map.put("b+b+b","valueB");
map.put("c-c-c","valueC");
request.setAttribute("map",map);
%>
${ map["a.a.a"] } <br>
${ map['b+b+b'] } <br>
${ map['c-c-c'] } <br>
</body>
五、EL 表達式的 11 個隐含對象
11 個隐含對象,EL表達式中自己定義的,可以直接使用
EL 擷取四個特定域中的屬性
pageScope ====== pageContext 域
requestScope ====== Request 域
sessionScope ====== Session 域
applicationScope ====== ServletContext 域
執行個體:
<body>
<%
pageContext.setAttribute("key1", "pageContext1");
pageContext.setAttribute("key2", "pageContext2");
request.setAttribute("key2", "request");
session.setAttribute("key2", "session");
application.setAttribute("key2", "application");
%>
${ applicationScope.key2 }
</body>
pageContext 對象的使用
- 協定:
- 伺服器 ip:
- 伺服器端口:
- 擷取工程路徑:
- 擷取請求方法:
- 擷取用戶端 ip 位址:
- 擷取會話的 id 編号:
執行個體
<body>
<%--
request.getScheme() 它可以擷取請求的協定
request.getServerName() 擷取請求的伺服器 ip 或域名
request.getServerPort() 擷取請求的伺服器端口号
getContextPath() 擷取目前工程路徑
request.getMethod() 擷取請求的方式( GET 或 POST )
request.getRemoteHost() 擷取用戶端的 ip 位址
session.getId() 擷取會話的唯一辨別
--%>
<%
pageContext.setAttribute("req", request);
%>
<%=request.getScheme() %> <br>
1.協定: ${ req.scheme }<br>
2.伺服器 ip:${ pageContext.request.serverName }<br>
3.伺服器端口:${ pageContext.request.serverPort }<br>
4.擷取工程路徑:${ pageContext.request.contextPath }<br>
5.擷取請求方法:${ pageContext.request.method }<br>
6.擷取用戶端 ip 位址:${ pageContext.request.remoteHost }<br>
7.擷取會話的 id 編号:${ pageContext.session.id }<br>
</body>
EL 表達式其他隐含對象的使用
.
param,Map<String,String> 它可以擷取請求參數的值
paramValues,Map<String,String[]> 它也可以擷取請求參數的值,擷取多個值的時候使用。
輸出請求參數 username 的值:${ param.username } <br>
輸出請求參數 password 的值:${ param.password } <br>
輸出請求參數 username 的值:${ paramValues.username[0] } <br>
輸出請求參數 hobby 的值:${ paramValues.hobby[0] } <br>
輸出請求參數 hobby 的值:${ paramValues.hobby[1] } <br>
請求位址:
http://localhost:8080/09_EL_JSTL/other_el_obj.jsp?username=wzg168&password=666666&hobby=java&hobby=cpp
.
header, Map<String,String> 它可以擷取請求頭的資訊
headerValues, Map<String,String[]> 它可以擷取請求頭的資訊,它可以擷取多個值的情況
執行個體
輸出請求頭中【User-Agent】的值:${ header['User-Agent'] } <br>
輸出請求頭中【Connection】的值:${ header.Connection } <br>
輸出請求頭中【User-Agent】的值:${ headerValues['User-Agent'][0] } <br>
.
cookie, Map<String,Cookie> 它可以擷取目前請求的 Cookie 資訊
執行個體
擷取 Cookie 的名稱:${ cookie.JSESSIONID.name } <br>
擷取 Cookie 的值:${ cookie.JSESSIONID.value } <br>
.
initParam,Map<String,String> 它可以擷取在 web.xml 中配置的上下文參數
web.xml 中的配置:
<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql:///test</param-value>
</context-param>
示例
輸出<Context-param>username 的值:${ initParam.username } <br>//root
輸出<Context-param>url 的值:${ initParam.url } <br>//jdbc:mysql:///test
總結:
要擷取隐含對象中對應的值,就在隐含對象後面加.點key就會輸出他的value值
.
JSTL标簽庫(次重點)
JSTL标簽庫,全稱: JSP Standard Tag Library
- 是一個不斷完善的開放源代碼的 JSP 标簽庫。EL 表達式主要是為了替換 jsp 中的表達式腳本,而标簽庫則是為了替換代碼腳本。這樣使得整個 jsp 頁面變得更佳簡潔。
- JSTL 由五個不同功能的标簽庫組成。
- 在 jsp 标簽庫中使用 taglib 指令引入标簽庫
CORE 标簽庫
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
XML 标簽庫
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
FMT 标簽庫
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
SQL 标簽庫
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
FUNCTIONS 标簽庫
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
一、 JSTL 标簽庫的使用步驟
1、先導入jstl标簽庫的jar包
taglibs-standard-impl-1.2.1.jar
taglibs-standard-spec-1.2.1.jar
2、使用taglib指令引入标簽庫
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
二、core 核心庫使用
1.<c:set /> (使用很少)
作用:set 标簽可以往域中儲存資料
<%--
<c:set />
作用: set 标簽可以往域中儲存資料
域對象 .setAttribute(key,value);
scope 屬性設定儲存到哪個域
page 表示 PageContext 域(預設值)
request 表示 Request 域
session 表示 Session 域
application 表示 ServletContext 域
var 屬性設定 key 是多少
value 屬性設定值
--%>
儲存之前:${ sessionScope.abc } <br>
<c:set scope="session" var="abc" value="abcValue"/>
儲存之後:${ sessionScope.abc } <br>
2. <c:if />
if 标簽用來做 if 判斷。
<%--
<c:if />
if 标簽用來做 if 判斷。
test 屬性表示判斷的條件(使用 EL 表達式輸出)
--%>
<c:if test="${ 12 == 12 }">
<h1>12 等于 12</h1>
</c:if>
<c:if test="${ 12 != 12 }">
<h1>12 不等于 12</h1>
</c:if>
3. < c:choose> < c:when> < c:otherwise> 标簽
作用:多路判斷。跟 switch … case … default 非常接近
<%--
iii.<c:choose> <c:when> <c:otherwise> 标簽
作用:多路判斷。跟 switch ... case .... default 非常接近
choose 标簽開始選擇判斷
when 标簽表示每一種判斷情況
test 屬性表示目前這種判斷情況的值
otherwise 标簽表示剩下的情況
<c:choose> <c:when> <c:otherwise> 标簽使用時需要注意的點:
1 、标簽裡不能使用 html 注釋,要使用 jsp 注釋
2 、 when 标簽的父标簽一定要是 choose 标簽
--%>
<%
request.setAttribute("height", 180);
%>
<c:choose>
<%-- 這是 html 注釋 --%>
<c:when test="${ requestScope.height > 190 }">
<h2>小巨人</h2>
</c:when>
<c:when test="${ requestScope.height > 180 }">
<h2>很高</h2>
</c:when>
<c:when test="${ requestScope.height > 170 }">
<h2>還可以</h2>
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${requestScope.height > 160}">
<h3>大于 160</h3>
</c:when>
<c:when test="${requestScope.height > 150}">
<h3>大于 150</h3>
</c:when>
<c:when test="${requestScope.height > 140}">
<h3>大于 140</h3>
</c:when>
<c:otherwise>
其他小于 140
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
4. <c:forEach />
作用:周遊輸出使用。
1、 周遊 1 到 10 ,輸出
<%--1. 周遊 1 到 10 ,輸出
begin 屬性設定開始的索引
end 屬性設定結束的索引
var 屬性表示循環的變量 ( 也是目前正在周遊到的資料 )
for (int i = 1; i < 10; i++)
--%>
<table >
<c:forEach begin="1" end="10" var="i">
<tr>
<td>第${i}行</td>
</tr>
</c:forEach>
</table>
2、周遊 Object 數組
<%-- 2. 周遊 Object 數組
for (Object item: arr)
items 表示周遊的資料源(周遊的集合)
var 表示目前周遊到的資料 命名可自己命名如item位置
--%>
<%
request.setAttribute("arr", new String[]{"18610541354","18688886666","18699998888"});
%>
<c:forEach items="${ requestScope.arr }" var="item">
${ item } <br>
</c:forEach>
3、 周遊 Map 集合
<%
Map<String,Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
// for ( Map.Entry<String,Object> entry : map.entrySet()) {
// }
request.setAttribute("map", map);
%>
<c:forEach items="${ requestScope.map }" var="entry">
<h1>${entry.key} = ${entry.value}</h1>
</c:forEach>
4、周遊 List 集合—list 放 中存放 Student 類 , 有屬性 : 編号 , 使用者名 , 密碼 , 年齡 ,
電話資訊
Student 類:
public class Student {
//編号,使用者名,密碼,年齡,電話資訊
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
}
<%--4. 周遊 List 集合 ---list 中存放 Student 類,有屬性:編号,使用者名,密碼,年齡,電話資訊 --%>
<%
List<Student> studentList = new ArrayList<Student>();
for (int i = 1; i <= 10; i++) {
studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i));
}
request.setAttribute("stus", studentList);
%>
<table>
<tr>
<th>編号</th>
<th>使用者名</th>
<th>密碼</th>
<th>年齡</th>
<th>電話</th>
<th>操作</th>
</tr>
<%--
items 表示周遊的集合
var 表示周遊到的資料
begin 表示周遊的開始索引值
end 表示結束的索引值
step 屬性表示周遊的步長值
varStatus 屬性表示目前周遊到的資料的狀态
for ( int i = 1; i < 10; i+=2 )
--%>
<c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.username}</td>
<td>${stu.password}</td>
<td>${stu.age}</td>
<td>${stu.phone}</td>
<td>${status.step}</td>
</tr>
</c:forEach>
</table>
感謝尚矽谷
學習資源來源:
https://www.bilibili.com/video/BV1Y7411K7zz