1 只可以獲取內置對象的屬性值,不可以獲取JSP頁面中局部java變量的值
<% String name = "James"; request.setAttribute("name",name); int age = 30; %> <h2>${name}</h2> <h2>${age}</h2>
2 只有“${”兩個字符連續出現時,才表示EL的開始,任何單獨字符出現時都可正常顯示
<% String name = "James"; request.setAttribute("name",name); %> <h2>${name}</h2> <h2>$${name}</h2> <h2>{${name}</h2>
3 如果只出現了“${”,而沒有“}”作為結束,則服務器報錯,出現空的“${}”時,服務器報錯
4 需要輸出“${”時,需要寫為“\${”(頁面最終顯示時會去掉“\”),或者寫成“${'${'}”
<h2>\${}</h2> <h2>${"${}"}</h2>
5 EL運算符中的“+”的操作數只可以是數字運算或者可以轉換為數字的字符串,對不可以轉換為數字的字符串運用“+”運算講產生錯誤
<h2>${123+"124"}</h2>
6 對於EL的empty運算符,null對象與空字符串“”、空數組、空list等是等價的
<% request.setAttribute("emptyString",""); request.setAttribute("nullObject",null); request.setAttribute("emptyList",new ArrayList<String>()); request.setAttribute("emptyMap",new HashMap<String,String>()); %> <h2>${empty emptyString}</h2> <h2>${empty nullObject}</h2> <h2>${empty emptyList}</h2> <h2>${empty emptyMap}</h2> </body>
7 EL獲取某個對象的值時,本質是調用該對象的toString()方法
<% request.setAttribute("requestString",request.toString()); %> <html> <body> <h2>${requestString}</h2> <h2>${pageContext.request}</h2> </body> </html>
8 EL的內置對象與JSP的內置對象並不相同(除了pageContext對象),兩者關系是:EL的內置對象可以訪問對應的JSP內置對象通過setAttribute方法存儲的值
- EL內置對象共有11個:pageContext、pageScope、requestScope、sessionScope、applicationScope、param、paramValues、header、headerValues、initParam、cookie(注意不存在responseScope,因為EL的本質是為了獲取某個值,而不是設置)
- JSP的內置對象共有9個:pageContext、page、resquest、response、session、application、out、config、exception
- 在頁面中直接使用${request}等會報錯
- 通過pageContext可以實現EL對JSP內置對象的獲取,${pageContext.request}
- 通過pageContext可以獲取的對象有page、resquest、response、session、out、exception、servletContext
- 不可以通過pageContext可以獲取的對象有application、config、pageContext
9 獲取JSP作用於范圍對象attribute的兩種方法
<% request.setAttribute("name","Shao"); %> <h2>${requestScope["name"]}</h2> <h2>${requestScope.name}</h2>
注意,下面的寫法是錯誤的,因為request對象並不存在getName方法
<h2>${pageContext.request.name}</h2>
10 獲取JSP作用於范圍對象屬性的方法
<h2>${pageContext.request.serverPort}</h2>