概念:
表達式語言(ExPRession Language,簡稱EL)是JSP 2.0中增加的新功能。使用表達式語言,可以方便的訪問標志位(在JSP中一共提供了四種標志位:page(pageContext)、request、session、application)中的屬性內(nèi)容,這樣就可以避免掉許多的scriptlet代碼,訪問的簡便語法:${屬性名稱}在不使用表達式語言,輸出屬性內(nèi)容<%@ page contentType="text/html" pageEncoding=“UTF-8"%><html><head><title>偶my耶</title></head><body><% request.setAttribute("info","www.oumuye.cn") ; // 設置一個request屬性 if (request.getAttribute("info") != null){ // 判斷是否有屬性存在%> <h3><%=request.getAttribute("info")%></h3> <!-- 輸出request屬性 --><% }%></body></html>
使用表達式語言,輸出屬性內(nèi)容
<%@ page contentType="text/html" pageEncoding="GBK"%><html><head><title>偶my耶</title></head><body><% request.setAttribute("info" ,"www.oumuye.cn") ; // 設置一個request屬性%><h3>${info}</h3> <!-- 表達式輸出 --></body></html>
由以上可知,EL表達式語言可以方便的進行屬性的輸出,使代碼更簡潔。
如果屬性不存在,則通過表達式語言自動將null設置為" ".。使用表達式可以輸出指定范圍的屬性或者是參數(shù)。表達式語言的內(nèi)置對象定義
屬性范圍
指定取出范圍的屬性
<%@ page contentType="text/html" pageEncoding="GBK"%><html><head><title>偶my耶</title></head><body><% pageContext.setAttribute("info","page屬性范圍") ; // 設置一個page屬性 request.setAttribute("info" , "request屬性范圍") ; // 設置一個request屬性 session.setAttribute("info" , "session屬性范圍") ; // 設置一個session屬性 application.setAttribute("info" , "application屬性范圍") ;// 設置一個application屬性%><h3>PAGE屬性內(nèi)容:${pageScope.info}</h3> <!-- 表達式輸出 --><h3>REQUEST屬性內(nèi)容:${requestScope.info}</h3> <!-- 表達式輸出 --><h3>SESSION屬性內(nèi)容:${sessionScope.info}</h3> <!-- 表達式輸出 --><h3>APPLICATION屬性內(nèi)容:${applicationScope.info}</h3> <!-- 表達式輸出 --></body></html>
調(diào)用JSP內(nèi)置對象的方法
<%@ page contentType="text/html" pageEncoding="GBK"%><html><head><title>偶my耶</title></head><body><h3>IP地址:${pageContext.request.remoteAddr}</h3><h3>SESSION ID:${pageContext.session.id}</h3><h3>是否是新session:${pageContext.session.new}</h3></body></html>
接收請求參數(shù)
<%@ page contentType="text/html" pageEncoding="GBK"%><html><head><title>偶my耶</title></head><body><h3>通過內(nèi)置對象接收輸入?yún)?shù):<%=request.getParameter("ref")%></h3><h3>通過表達式語言接收輸入?yún)?shù):${param.ref}</h3></body></html>
效果圖
使用表達式接收一組參數(shù)
<html><head><title>偶my耶</title></head><body><form action="param_values_demo.jsp" method="post"> 興趣: <input type="checkbox" name="inst" value="唱歌">唱歌 <input type="checkbox" name="inst" value="游泳">游泳 <input type="checkbox" name="inst" value="看書">看書 <input type="submit" value="顯示"></form></body></html>
param_values_demo.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%><html><head><title>偶my耶</title></head><body><% request.setCharacterEncoding("GBK") ; %><h3>第一個參數(shù):${paramValues.inst[0]}</h3><h3>第二個參數(shù):${paramValues.inst[1]}</h3><h3>第三個參數(shù):${paramValues.inst[2]}</h3></body></html>默認情況下表達式是采用順序的方式輸出屬性的: 順序:page > request > session > application在表達式語言中也已經(jīng)很好的支持了集合的操作,可以方便的使用表達式語言輸出Collection(子接口:List、Set)、Map集合中的內(nèi)容。輸出Collection接口集合
<%@ page contentType="text/html" pageEncoding="GBK" import="java.util.*"%><html><head><title>偶my耶</title></head><body><% List all = new ArrayList() ; // 實例化List接口 all.add(“偶my耶”) ; // 向集合中增加內(nèi)容 all.add(“oumyye”) ; // 向集合中增加內(nèi)容 all.add(“oumyye@163.com”) ; // 向集合中增加內(nèi)容 request.setAttribute(“allinfo”,all) ; // 向request集合中保存%><h3>第一個元素:${allinfo[0]}</h3><h3>第二個元素:${allinfo[1]}</h3><h3>第三個元素:${allinfo[2]}</h3></body></html>
輸出Map集合
Map集合輸出時依然要通過KEY找到VALUE
<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import="java.util.*"%><html><head><title>偶my耶</title></head><body><% Map map = new HashMap() ; map.put("omy","oumyye") ; map.put("zsy","偶my耶") ; map.put("email","oumyye@163.com") ; request.setAttribute("info",map) ; // 集合保存在request范圍%><h3>KEY為omy的內(nèi)容:${info["omy"]}</h3><h3>KEY為zsy的內(nèi)容:${info["zsy"]}</h3><h3>KEY為email的內(nèi)容:${info["email"]}</h3></body></html>
新聞熱點
疑難解答
圖片精選