EL表達式
1.EL:expression Language
EL語法很簡單,它最大的特點就是使用上很方便,EL的主要語法結構
${sessionScope.user.age}
從Session範圍中取得用戶的年齡
所有的EL都是以${爲起始,以}
爲結尾的
2.EL提供.和[]兩種運算符來存取數據。如:${sessionScope.user.age}
等同於${sessionScope.user["age"]}
3.EL可以進行自動的類型轉換如:${param.score + 11}
4.EL中的隱含對象如:${user.score }
將會從page開始找user直到application中爲止。
隱含對象範圍:pageScope,requestScope,sessionScope,applicationScope
如:${applicationScope.time}
直接在application範圍內
輸入參數有關的${param.name}
獲取一個請求參數,${paramValues.name}
獲取一組請求參數
其他隱含對象:pageContext等(cookie,header,initParam)。
5.EL關係運算符${param.age > 18 ? "adult":"children"}
EL的empty可以作用於一個集合,若該集合不存在或集合中沒有元素,其結果都爲true
name is empty:${empty requestScope.name}
重點代碼示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>El表達式</title>
</head>
<body>
例如:pageContext:<br>
contextPath:${pageContext.request.contextPath}
sessionId:${pageContext.session.id}
sessionAttributeNames:${pageContext.session.attributeNames}
<form action="EL.jsp"method="post">
//用EL表達式可以方便的實現回顯
username:<input type="text"name="username"value="${param.username}"/>
<input type="submit"value="Submit"/>
</form>
</body>
</html>