【EL的概述】
EL:Expression Language:表達式語言
簡化JSP的代碼,而且減少<%%>
語法:${EL 表達式}
EL的功能:
*獲取數據的:(JSP的四個域)
*執行運算
*操作web開發的常用的對象
*調用Java中的方法(不常用)
【獲取數據】
<h1>EL功能:獲取數據</h1>
<%
pageContext.setAttribute("pname", "pvalue");
request.setAttribute("rname", "rvalue");
session.setAttribute("sname", "svalue");
application.setAttribute("aname", "avalue");
%>
<%= pageContext.getAttribute("pageNmae") %><!-- 如果找不到就返回NUll -->
<hr>
${ pageScope.pname } <!-- 如果找不到,就返回“”,空值 -->
${ requestScope.rname}
${ sessionScope.pname }
${ applicationScope.rname}
獲取數組
<%
String[] arrs = {"tommy","allen","hans"};
pageContext.setAttribute("arrs", arrs);
%>
${ pageScope.arrs[0] }
獲取List集合
<!-- 招數據,先從page-request-session-application -->
<hr>
<%
String[] arrs = {"tommy","allen","hans"};
pageContext.setAttribute("arrs", arrs);
%>
${ pageScope.arrs[0] }
<%
List<String> list = new ArrayList<String>();
list.add("tommy");
list.add("allen");
pageContext.setAttribute("list", list);
%>
${ list[0] }
${ list[1] }
<hr/>
<%
Map map = new HashMap();
map.put("aaa", "tommy");
map.put("bbb", "allen");
pageContext.setAttribute("map", map);
%>
${ map.aaa }
${ map.bbb }
* . 和[] 的區別
[] 用於有下標的數據(數組,List集合),用於有屬性的數據(map,對象)
如果屬性名中包含特殊的字符,必須使用[]
<hr/>
<%
Map map = new HashMap();
map.put("aaa", "tommy");
map.put("bbb", "allen");
pageContext.setAttribute("map", map);
%>
${ map.aaa }
${ map.bbb }
${ map["ddd.eee"] }
<hr/>
<!-- 獲得對象的屬性值 -->
<%
User user = new User(1,"aaa","123");
pageContext.setAttribute("user", user);
%>
${ user.id }
${ user.username }
${ user.password }
<h3>獲取對象集合的數據</h3>
<%
User user1 = new User(1,"aaa","123");
User user2 = new User(1,"bbb","123");
User user3 = new User(1,"ccc","123");
List userlist = new ArrayList();
userlist.add(user1);
userlist.add(user2);
userlist.add(user3);
pageContext.setAttribute("userlist", user);
%>
${ userlist[0].id } - ${ userlist[0].username } - ${ userlist[0].password }
${ userlist[1].id } - ${ userlist[1].username } - ${ userlist[1].password }
${ userlist[2].id } - ${ userlist[2].username } - ${ userlist[2].password }
【EL執行運算】
【執行數學運算】
<% pageContext.setAttribute("n1", 10);
pageContext.setAttribute("n2", 20);
%>
${ n1+n2 }
<h1>el執行邏輯運算</h1>
${ n1 < n2 } - ${ n1 lt n2 } <!-- lt = less than -->
${ n1 <= n2 } - ${ n1 le n2 } <!-- le = less equal -->
${ n1 > n2 } - ${ n1 gt n2 } <!-- great than -->
${ n1 >= n2 } - ${ n1 ge n2 } <!-- great equal -->
${ n1 == n2 } - ${ n1 eq n2 }
<h1>el不能執行關係運算</h1>
${ n1 < n2 && n3 < n4 } - ${ n1 lt n2 and n3 lt n4 }
${ n1 < n2 || n3 < n4 } - ${ n1 lt n2 or n3 lt n4 }
${ !(n1 < n2) } - ${ not(n1 lt n2) }
<h1>el執行三元運算</h1>
${ n1 < n2 ? "true" : "false" }
<h1>empty 運算</h1>
${ user == null } - ${ empty user }
${ user != null } - ${ not empty user }
【El操作Web開發的常用對象】
<h1>操作web開發的常用的對象</h1>
<!-- pageScope,requestScope,sessionScope,applicationScope,param,paramValues -- 接受參數 -->
<!-- header,headerValues -- 獲取請求體 -->
<!-- initParmm -- 獲取全局初始化參數 -->
<!-- cookie -- WEB開發中cookies -->
<!-- pageContext -- Web 開發中的pageContext -->
<%
//獲得其他8個內置對象 --- 主要在編寫框架,通用性很高的代碼中
request.setAttribute("id", "001");
request.setAttribute("name", "name");
%>
<%= request.getAttribute("id") %>
<%= request.getAttribute("name") %>
<!-- 接受請求的參數 -->
<%= request.getParameter("ids") %>
<%= request.getParameter("names") %>
<%= request.getParameter("hobbies") %>
<hr>
${ requestScope.id }
${ requestScope.name }
<!-- http://localhost:8080/day12JSP/demo05-EL/ELDemo3.jsp?ids=001&names=zuochunhui -->
${ requestScope.ids }
${ requestScope.names }
${ paramValues.hobbies[0] }
${ paramValues.hobbies[1] }
<!-- 001 zuochunhui 001 name basket football -->
<hr>
<!-- 獲取請求頭 -->
<%= request.getHeader("User-Agent") %>
<hr>
${ header["User-Agent"] }
<!-- Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko -->
<h3>獲取全局初始化參數</h3>
<!-- <context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
-->
${ initParam.username }
${ initParam.value }
<h3>獲取cookies中的值</h3>
${ cookie.history.value }
<h3>pageContext中的對象</h3>
<!-- get Ip address -->
${ pageContext.request.remoteAddr }
<!-- 獲取工程路徑 -->
${ pageContext.request.contextPath }