# EL表達式
EL能夠極大的簡化我們的開發
EL 全名為 Expression Language,它原本是 JSTL 1.0 為方便存取數據所自定義的語言。當時 EL只能在JSTL標簽中使用。到了JSP2.0 之后,EL已經正式納入成為標準規范之一。
## 語法
EL 語法很簡單,它最大的特點就是使用上很方便。接下來介紹 EL 主要的語法結構
~~~
${sessionScope.user.sex}
~~~
所有 EL 都是以 ${ 為起始、以} 為結尾的。上述 EL范例的意思是:從 Session 的范圍中,取得用戶的性別。假若依照之前的寫法如下:
~~~
User user = (User)session.getAttribute("user");
String sex = user.getSex( );
~~~
兩者相比較之下,可以發現 EL 的語法比傳統 JSP更為方便、簡潔。
### .與 [ ] 運算符
多數情況下使用.,特殊情況使用[ ]
~~~
<%@page import="com.neusoft.mvcapp.dao.Person"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Person person = new Person();
person.setName("neusoft");
session.setAttribute("com.neusoft.person", person);
%>
name:${sessionScope["com.neusoft.person"].name }
</body>
</html>
~~~
### EL變量
EL 存取變量數據的方法很簡單,例 如 :${username}。它的意思是取出某一范圍中名稱為 username的變量。因為我們并沒有指定哪一個范圍的 username,所以它的默認值會先從 Page 范圍找,假如找不到,再依序到 Request、Session、Application 范圍。假如途中找到 username,就直接回傳,不再繼續找下去,但是假如全部的范圍都沒有找到時,就回傳null。

***
### 自動類型轉換
el.jsp
~~~
<%@page import="com.neusoft.mvcapp.dao.Person"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="el2.jsp?score=20">To EL2 Page</a>
</body>
</html>
~~~
el2.jsp
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${ param.score+30 }
<%= request.getParameter("score")+30 %>
</body>
</html>
~~~
### EL 隱含對象
①與范圍有關的隱含對象
applicationScope
sessionScope
requestScope
pageScope
舉一個例子,其他同理
el.jsp
~~~
<%@page import="com.neusoft.mvcapp.dao.Person"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="el2.jsp?score=20">To EL2 Page</a>
<%
application.setAttribute("app", "appValue");
%>
</body>
</html>
~~~
el2.jsp
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${ app }<br>
${ applicationScope.app }
</body>
</html>
~~~
②與輸入有關的隱含對象
param
~~~
${ param.score }
<%= request.getParameter("score") %>
~~~
paramValues
~~~
${ paramValues.score[1] }
<%= request.getParameterValues("score")[1] %>
~~~
EL本身沒有遍歷paramValues的能力,JSTL可以,之后講
如果獲取的參數還有無參的get方法,可以一直使用.找到需要的方法,例如:
~~~
${ requestScope.user.age }
~~~
③其他的隱含對象
cookie
~~~
${ cookie.JESESSIONID.name } -- ${ cookie.JESESSIONID.value }
~~~
initParam
~~~
${ initParam.initName }
~~~
**pageContext**
~~~
${ pageContext.request.contextPath }
~~~
- 第一章 配置和安裝Tomcat
- 第二章 Servlet(一)
- 第三章 Servlet(二)
- 練習 一 . Servlet配置級獲取初始化參數
- 第四章 JSP(一)
- 第五章 JSP(二)
- 第六章 MVC設計模式
- 第七章 Cookie
- 第八章 Session
- 練習 二 . 簡易版購物車
- 第九章 EL表達式
- 第十章 JSTL
- 第十一章 過濾器
- 第十二章 監聽器
- 第十三章 文件的上傳與下載
- 復習總結
- 如何手動啟動Tomcat
- 如何修改Tomcat端口號
- 如何在web.xml中配置Servlet
- Servlet生命周期
- load-on-startup參數
- Servlet映射路徑
- POST和GET的區別
- JSP中9個隱式對象及功能
- 請求轉發及請求重定向的區別
- JSP指令有哪些
- 簡述對MVC設計模式的理解
- 簡述Cookie機制
- 簡述Session機制
- HttpSession的生命周期
- Cookie和Session有什么區別
- 簡述創建過濾器步驟
- 過濾器經典案例--統一編碼字符集
- getParameter與getAttribute的區別
- JSP頁面中可以包含哪些元素
- web應用中,是如何跟蹤用戶的
- InteliJ創建web項目