第四節:SignalR大雜燴(與MVC融合、全域性的幾個配置、跨域的應用、C/S程式充當Client和Server)
阿新 • • 發佈:2020-11-04
目錄
概念
EL使JSP寫起來更簡單、簡潔。主要用於獲取作用域中的資料。
作用
用於替換作用域物件.getAttribute("name");
EL的應用
${scope.name} 獲取集體某個作用域中的資料
${name} 獲取作用域中的資料,逐級查詢(pageContext、request、session、application)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>el表示式</title> </head> <body> <% request.setAttribute("key1","value1"); session.setAttribute("key2","value2"); application.setAttribute("key3","value3"); %> <%--通過域物件獲取值--%> <%=request.getAttribute("key1")%><br> <%=session.getAttribute("key2")%><br> <%=application.getAttribute("key3")%><br> <hr/> <%--el表示式獲取值--%> ${requestScope.key1}<br> ${sessionScope.key2}<br> ${applicationScope.key3}<br> <hr/> <%--el表示式簡寫,不推薦 會從requestScope、sessionScope、applicationScope 搜尋值,找不到不返回結果 --%> ${key1}<br> ${key2}<br> ${key3}<br> ${key4}<br> </body> </html>
EL獲取引用型別
使用EL獲取作用域中的物件呼叫屬性時,只能訪問物件的get方法,必須遵守命名規範定義
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% User user = new User("張三",18); request.setAttribute("user",user); %> ${requestScope.user.name}<br> ${requestScope.user.age}<br><!--呼叫get方法--> </body> </html>
EL獲取陣列、集合的元素
EL可以獲取Array、List、Map中的元素,Set用於沒下標,無法直接訪問,後續可遍歷。
<%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>array</title> </head> <body> <% int[] a = {1,2,3,4}; ArrayList<Integer> arrayList = new ArrayList<>(); arrayList.add(1); arrayList.add(2); arrayList.add(3); Map<String,String> map = new HashMap<>(); map.put("河南","豫"); map.put("北京","京"); map.put("上海","滬"); //向域中存入陣列,集合 request.setAttribute("a",a); request.setAttribute("arrayList",arrayList); request.setAttribute("map",map); %> ${a[0]}<br> ${a[1]}<br> ${a[2]}<br> ${a[4]}<br><hr/> ${arrayList[0]}<br> ${arrayList.get(1)}<br> ${arrayList[2]}<br><hr/> ${map.get("河南")}<br> ${map.北京}<br> ${map["上海"]}<br> </body> </html>
EL的運算子
操作符 | 描述 |
---|---|
. | 訪問一個Bean屬性或者一個對映條目 |
[] | 訪問一個數組或連結串列的元素 |
+ | 加 |
- | 減 |
* | 乘 |
/ or div | 除 |
% or mod | 取模 |
== or eq | 等於 |
!= or ne | 不等於 |
< or lt | 小於 |
> or gt | 大於 |
<= or le | 小於等於 |
>= or ge | 大於等於 |
&& or and | 與 |
|| or or | 或 |
! or not | 取反 |
empty | 是否為空(""或null) |
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>運算子</title>
</head>
<body>
${ 2 +3 }
${ 2 -3 }
${ 2 *3 }
${ 2 / 3 }
${ 2 % 3 }
<hr/>
${ 2 > 3 }
${ 2 >= 3 }
${ 2 < 3 }
${ 2 <= 3 }
${ 2 != 3 }
<hr/>
${true && true}
${true || true}
<hr/>
${empty null}
</body>
</html>
隱式物件
隱式物件 | 描述 |
---|---|
pageScope | page作用域 |
requestScope | request作用域 |
sessionScope | session作用域 |
applicationScope | application作用域 |
param | Request物件的引數,字串 |
paramValues | Request物件的引數,字串集合 |
header | HTTP資訊頭,字串 |
headerValues | HTTP資訊頭,字串集合 |
initParam | 上下文初始化引數 |
cookie | Cookie值 |
pageContext | 當前頁面的pageContext |
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--獲取專案路徑--%>
${pageContext.request.contextPath}<br>
<%--獲取cookie--%>
<input type="text" name="username" value="${cookie.username.value}">
<input type="password" name="password" value="${cookie.password.value}">
</body>
</html>