目錄
- JSP中的EL 運算式
- 什么是 EL 運算式,EL 運算式的作用?
- EL 運算式搜索域資料的順序
- EL 運算式輸出 Bean 的普通屬性,陣列屬性,List 集合屬性,map 集合屬性
- EL 運算式 --運算
- 關系運算
- 邏輯運算
- 算術運算
- empty 運算
- 三元運算
- “ . ”點運算 和 [] 中括號運算子
- EL 運算式的 11 個隱含物件
- EL 獲取四個特定域中的屬性、
- pageContext 物件的使用
- EL 運算式其他隱含物件的使用
JSP中的EL 運算式
什么是 EL 運算式,EL 運算式的作用?
EL 運算式的全稱是:Expression Language,是運算式語言,
EL 運算式的什么作用:EL 運算式主要是代替 jsp 頁面中的運算式腳本在 jsp 頁面中進行資料的輸出,
因為 EL 運算式在輸出資料的時候,要比 jsp 的運算式腳本要簡潔很多,
<body>
<%
request.setAttribute("key","值");
%>
運算式腳本輸出 key 的值是:
<%=request.getAttribute("key")==null?"":request.getAttribute("key")%><br/>
EL 運算式輸出 key 的值是:${key}
</body>

EL 運算式的格式是:${運算式}
EL 運算式在輸出 null 值的時候,輸出的是空串,jsp 運算式腳本輸出 null 值的時候,輸出的是 null 字串,
EL 運算式搜索域資料的順序
EL 運算式主要是在 jsp 頁面中輸出資料,
主要是輸出域物件中的資料,
當四個域中都有相同的 key 的資料的時候,EL 運算式會按照四個域的從小到大的順序去進行搜索,找到就輸出,
<body>
<%
// 往四個域中都保存了相同的 key 的資料,
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
pageContext.setAttribute("key", "pageContext");
%>
${ key }
</body>

注釋掉pageContext:

四個域的大小:pageContext<request<session<application
按照從小到大的順序進行搜索然后輸出,
EL 運算式輸出 Bean 的普通屬性,陣列屬性,List 集合屬性,map 集合屬性
例:輸出 Person 類中普通屬性,陣列屬性,list 集合屬性和 map 集合屬性,
Person 類:
public class Person {
// Person 類中普通屬性,陣列屬性, list 集合屬性和 map 集合屬性,
private String name;
private String[] phones;
private List<String> cities;
private Map<String,Object> map;
public Person() {
}
public int getAge() {
return 21;
}
public void setName(String name) {
this.name = name;
}
public void setPhones(String[] phones) {
this.phones = phones;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public String getName() {
return name;
}
public String[] getPhones() {
return phones;
}
public List<String> getCities() {
return cities;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public Person(String name, String[] phones, List<String> cities, Map<String, Object> map) {
this.name = name;
this.phones = phones;
this.cities = cities;
this.map = map;
}
}
jsp輸出代碼:
<%
Person person = new Person();
person.setName("愷龍");
person.setPhones(new String[]{"18755556666","18688886666","18699998888"});
List<String> cities = new ArrayList<String>();
cities.add(" 大連");
cities.add(" 沈陽");
cities.add(" 北京");
person.setCities(cities);
Map<String,Object> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
person.setMap(map);
pageContext.setAttribute("p", person);
%>
輸出 Person:${ p }<br/>
輸出 Person 的 name 屬性:${p.name} <br>
輸出 Person 的 phones 陣列屬性值:${p.phones[2]} <br>
輸出 Person 的 cities 集合中的元素值:${p.cities} <br>
輸出 Person 的 List 集合中個別元素值:${p.cities[2]} <br>
輸出 Person 的 Map 集合: ${p.map} <br>
輸出 Person 的 Map 集合中某個 key 的值: ${p.map.key3} <br>
輸出 Person 的 age 屬性:${p.age} <br>
結果:

EL 運算式 --運算
語法:${ 運算運算式 } , EL 運算式支持如下運算子:
關系運算
| 關系運算子 | 說明 | 范 例 | 結果 |
|---|---|---|---|
| == 或 eq | 等于 | ${ 5 == 5 } 或 ${ 5 eq 5 } | true |
| != 或 ne | 不等于 | ${ 5 !=5 } 或 ${ 5 ne 5 } | false |
| < 或 lt | 小于 | ${ 3 < 5 } 或 ${ 3 lt 5 } | true |
| > 或 gt | 大于 | ${ 2 > 10 } 或 ${ 2 gt 10 } | false |
| <= 或 le | 小于等于 | ${ 5 <= 12 } 或 ${ 5 le 12 } | true |
| >= 或 ge | 大于等于 | ${ 3 >= 5 } 或 ${ 3 ge 5 } | false |
邏輯運算
| 邏輯運算子 | 說 明 | 范 例 | 結果 |
|---|---|---|---|
| && 或 and | 與運算 | ${ 12 == 12 && 12 < 11 } 或 ${ 12 == 12 and 12 < 11 } | false |
| || 或or | 或運算 | ${12 == 12 || 12<11} 或 ${12==12or12<11} | true |
| !或not | 取反運算 | ${ !true } 或 ${not true } | false |
算術運算
| 算術運算子 | 說明 | 范例 | 結果 |
|---|---|---|---|
| + | 加法 | ${ 12 + 18 } | 30 |
| - | 減法 | ${ 18 - 8 } | 10 |
| * | 乘法 | ${ 12 * 12 } | 144 |
| /或div | 除法 | ${ 144 / 12 } 或 ${ 144 div 12 } | 12 |
| %或mod | 取模 | ${ 144 % 10 } 或 ${ 144 mod 10 } | 4 |
empty 運算
empty 運算可以判斷一個資料是否為空,如果為空,則輸出 true,不為空輸出 false,
以下幾種情況為空:
1、值為 null 值的時候,為空
2、值為空串的時候,為空
3、值是 Object 型別陣列,長度為零的時候
4、list 集合,元素個數為零
5、map 集合,元素個數為零
<body>
<%
// 1 、值為 null 值的時候,為空
request.setAttribute("emptyNull", null);
// 2 、值為空串的時候,為空
request.setAttribute("emptyStr", "");
// 3 、值是 Object 型別陣列,長度為零的時候
request.setAttribute("emptyArr", new Object[]{});
// 4 、 list 集合,元素個數為零
List<String> list = new ArrayList<>();
request.setAttribute("emptyList", list);
//list 集合,元素個數為零
List<String> list1 = new ArrayList<>();
list1.add("abc");
request.setAttribute("emptyList1",list1);
// 5 、 map 集合,元素個數為零
Map<String,Object> map = new HashMap<String, Object>();
request.setAttribute("emptyMap", map);
map 集合,元素個數為1
Map<String,Object> map1 = new HashMap<String, Object>();
map1.put("key1", "value1");
request.setAttribute("emptyMap1", map1);
%>
${ empty emptyNull } <br/>
${ empty emptyStr } <br/>
${ empty emptyArr } <br/>
${ empty emptyList } <br/>
${ empty emptyList1 } <br/>
${ empty emptyMap } <br/>
${ empty emptyMap1 } <br/>
</body>
結果:

三元運算
運算式 1?運算式 2:運算式 3
如果運算式 1 的值為真,回傳運算式 2 的值,如果運算式 1 的值為假,回傳運算式 3 的值,
示例:
${ 12 != 12 ? "運算式為真":" 運算式為假" }
“ . ”點運算 和 [] 中括號運算子
.點運算,可以輸出 Bean 物件中某個屬性的值,
[]中括號運算,可以輸出有序集合中某個元素的值,
并且[]中括號運算,還可以輸出 map 集合中 key 里含有特殊字符的 key 的值,
<body>
<%
Map<String,Object> map = new HashMap<String, Object>();
map.put("a.a.a", "aaaValue");
map.put("b+b+b", "bbbValue");
map.put("c-c-c", "cccValue");
map.put("d", "dValue");
map.put("e", "eValue");
map.put("f", "fValue");
request.setAttribute("map", map);
%>
${ map['a.a.a'] } <br>
${ map["b+b+b"] } <br>
${ map['c-c-c'] } <br>
${ map.d } <br>
${ map.e} <br>
${ map.f} <br>
</body>
結果:

EL 運算式的 11 個隱含物件
EL 個達式中 11 個隱含物件,是 EL 運算式中自己定義的,可以直接使用,
| 變數 | 型別 | 作用 |
|---|---|---|
| pageContext | PageContextImpl | 它可以獲取 jsp 中的九大內置物件 |
| pageScope | Map<String,Object> | 它可以獲取 pageContext 域中的資料 |
| requestScope | Map<String,Object> | 它可以獲取 Request 域中的資料 |
| sessionScope | Map<String,Object> | 它可以獲取 Session 域中的資料 |
| applicationScope | Map<String,Object> | 它可以獲取 ServletContext 域中的資料 |
| param | Map<String,String> | 它可以獲取請求引數的值 |
| paramValues | Map<String,String[]> | 它也可以獲取請求引數的值,獲取多個值的時候使用, |
| header | Map<String,String> | 它可以獲取請求頭的資訊 |
| headerValues | Map<String,String[]> | 它可以獲取請求頭的資訊,它可以獲取多個值的情況 |
| cookie | Map<String,Cookie> | 它可以獲取當前請求的 Cookie 資訊 |
| initParam | Map<String,String> | 它可以獲取在 web.xml 中配置的 |
例子:
<body>
<%
request.setAttribute("aaa","aaaValue");
session.setAttribute("bbb","bbbValue");
%>
${requestScope["aaa"] }<br>
${sessionScope["bbb"]}<br>
</body>
結果:

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 }
${ sessionScope.key2 }
${ requestScope.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[]> | 它也可以獲取請求引數的值,獲取多個值的時候使用 |
假設瀏覽器中請求地址為:
http://localhost:8080/JSPDemo/PersonOutput.jsp?username=愷龍&password=123456&teacher=耿祥義&teacher=柳淑琴
<body>
輸出請求引數 username 的值:${ param.username } <br>
輸出請求引數 password 的值:${ param.password } <br>
輸出請求引數 username 的值:${ paramValues.username[0] } <br>
輸出請求引數 teacher 的值:${ paramValues.teacher[0] } <br>
輸出請求引數 teacher 的值:${ paramValues.teacher[1] } <br>
</body>
結果:

| 變數 | 型別 | 作用 |
|---|---|---|
| header | Map<String,String> | 它可以獲取請求頭的資訊 |
| headerValues | Map<String,String[]> | 它可以獲取請求頭的資訊,它可以獲取多個值的情況 |
<body>
輸出請求頭【User-Agent】的值:${ header['User-Agent'] } <br>
輸出請求頭【Connection】的值:${ header.Connection } <br>
輸出請求頭【User-Agent】的值:${ headerValues['User-Agent'][0] } <br>
</body>

| 變數 | 型別 | 作用 |
|---|---|---|
| cookie | Map<String,Cookie> | 它可以獲取當前請求的 Cookie 資訊 |
<body>
獲取 Cookie 的名稱:${ cookie.JSESSIONID.name } <br>
獲取 Cookie 的值:${ cookie.JSESSIONID.value } <br>
</body>

| 變數 | 型別 | 作用 |
|---|---|---|
| initParam | Map<String,String> | 它可以獲取在 web.xml 中配置的 |
web.xml 中的配置:
<context-param>
<param-name>username</param-name>
<param-value>愷龍</param-value>
</context-param>
<context-param>
<param-name>age</param-name>
<param-value>21</param-value>
</context-param>
jsp代碼:
<body>
輸出<Context-param>username 的值:${ initParam.username } <br>
輸出<Context-param>age 的值:${ initParam.age } <br>
</body>

歡迎關注公眾號:愚生淺末

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/502963.html
標籤:Java
上一篇:Spring5.0學習知識總結
下一篇:JSP中的JSTL 標簽庫
