1. 程式人生 > >EL表達式總結

EL表達式總結

pty 等於 == sessions 調用 學習 數據 reat 代碼

EL表達式是在JSP中使用的

EL表達式的作用:簡化jsp文件中的<% %>。

【EL的概述】

 1     什麽是EL:
 2  
 3     為什麽學習EL:
 4 * 簡化JSP的代碼,而且減少<%%>
 5     使用EL表達式:
 6 * 語法:${ EL表達式 }
 7     EL的功能:
 8 * 獲取數據:(JSP的四個域)獲取的數據必須存放到jsp的四個域中,jsp四個域分別是pageContext  request  session  application
 9 * 執行運算:
10 * 操作WEB開發的常用的對象:
11 * 調用Java中方法:--很少用.

【EL獲取數據】

 1 <h3>存取是普通的單值數據</h3>
//<% %>中寫的是java代碼 2 <% //往jsp文件中四個域分別的存入數據 , “name”是域的名稱,“Value”是存入的值。 3 pageContext.setAttribute("name", "pValue"); 4 request.setAttribute("name", "rValue"); 5 session.setAttribute("name", "sValue"); 6 application.setAttribute("name", "aValue");
7 %>
//分別獲取四個域中的值(傳統的jsp獲取寫法) 8 <%=pageContext.getAttribute("name") %> <!-- 如果沒找到 返回null --> 9 <%=request.getAttribute("name") %> 10 <%=session.getAttribute("name") %> 11 <%=application.getAttribute("name") %> 12 <hr/> //是一條實線 13 ${ pageScope.name } <!-- 返回的是"" --> //獲取的是pageContext域中的名稱為name的值 14
${ requestScope.name } //獲取的是request域中的名稱為name的值 15 ${ sessionScope.name } //獲取的是session域中的名稱為name的值 16 ${ applicationScope.name } //獲取的是application域中的名稱為name的值 17 <hr/> 18 ${ name } <!-- 類似findAttribute("name") 先從page域中查找,沒找到去request域中查詢,沒有找到去session域中找,沒有找到就去application域中找 --> 19 <h3>獲取數組的數據</h3> 20 <% 21 String[] arrs = {"李旭華","李冠希","楊鳳","楊如花"}; 22 pageContext.setAttribute("arrs", arrs); //將數組存入到pageContext域中 名稱為“arrs”。 23 %>
//分別獲取數組中的元素。 24 ${ arrs[0] } 25 ${ arrs[1] } 26 ${ arrs[2] } 27 ${ arrs[3] } 28 <h3>獲取List集合的數據</h3> 29 <%
//創建一個集合 30 List<String> list = new ArrayList<String>();
//往集合中添加數據
31 list.add("李芙蓉"); 32 list.add("楊芙蓉"); 33 list.add("王鳳");
//將集合存到pageContext域中,“list”是這個域的名稱,list是添加到這個域中的值
34 pageContext.setAttribute("list", list); 35 %>
//使用EL表達式來獲取這些值 36 ${ list[0] } 37 ${ list[1] } 38 ${ list[2] } 39 <h3>獲取Map集合的數據</h3> 40 <%
//創建一個map集合 41 Map<String,String> map = new HashMap<String,String>();
//往map集合中添加數據
42 map.put("aaa","李旭華"); 43 map.put("bbb","楊久君"); 44 map.put("ccc","李芮"); 45 map.put("ddd","李鳳");
//將map集合添加到pageContext域中,“map”是名字 ,map是添加到域中的值 也就是我們創建的map集合
46 pageContext.setAttribute("map", map); 47 %>
//使用EL表達式來獲取map集合中的值。 需要註意的是 當需要獲取的是其中的屬性,那麽用. 如果獲取的是裏邊的索引那麽則使用[]。 48 ${ map.aaa } 49 ${ map.bbb } 50 ${ map.ccc } 51 ${ map.ddd }
52 <h3>獲取對象的數據</h3> 53 <%
//創建一個user對象 54 User user = new User(1,"aaa","123");
//將對象存入到pageContext域中 域名叫“user”,域值是user
55 pageContext.setAttribute("user", user); 56 %>
//使用EL表達式來分別獲取id username password 其中username 和password寫在了userBean中這裏代碼就不寫了。 57 ${ user.id } 58 ${ user.username } 59 ${ user.password } 60 <h3>獲取對象的集合的數據</h3> 61 <%
//創建3個user對象 62 User user1 = new User(1,"aaa","123"); 63 User user2 = new User(2,"bbb","123"); 64 User user3 = new User(3,"ccc","123"); 65 //創建一個存入上邊3個對象的集合 66 List<User> userList = new ArrayList<User>();
//分別將3個user對象存入到userlist集合中
67 userList.add(user1); 68 userList.add(user2); 69 userList.add(user3); 70 //將userlist集合存入到pageContext域中,域名叫“userlist”,域值叫userlist. 71 pageContext.setAttribute("userList", userList); 72 %> 73   //使用EL表達式分別獲取三個對象的值。 74 ${ userList[0].id } - ${ userList[0].username } - ${ userList[0].password }<br/> 75 ${ userList[1].id } - ${ userList[1].username } - ${ userList[1].password }<br/> 76 ${ userList[2].id } - ${ userList[2].username } - ${ userList[2].password }<br/> 77 ***** .和[]的區別. 78 * []用於有下標的數據(數組,list集合) .用於有屬性的數據(map,對象) 79 * 如果屬性名中包含有特殊的字符.必須使用[“”]

【EL執行運算】

<h1>EL的功能二:執行運算</h1>
<h3>EL執行算數運算</h3>
<%
  //往pageContext域中添加四個域 pageContext.setAttribute("n1", "10"); pageContext.setAttribute("n2", "20"); pageContext.setAttribute("n3", "30"); pageContext.setAttribute("n4", "40"); %> //利用EL表達式進行算數運算 ,會進行類型轉化,並且當加的值沒有參與這個運算 則智能的忽略的n4. 總之很智能。
${ n1
+ n2 + n3 } <h3>EL執行邏輯運算</h3> //返回的是boolean類型的 打印的是true或者false ${ n1 < n2 } - ${ n1 lt n2 } <!-- less than --><br/> //小於 ${ n1 > n2 } - ${ n1 gt n2 } <!-- great than --><br/> //大於 ${ n1 <= n2 } - ${ n1 le n2 } <!-- less equal --><br/> //小於等於 ${ n1 >= n2 } - ${ n1 ge n2 } <!-- great equal --><br/>//大於等於 ${ n1 == n2 } - ${ n1 eq n2 } <!-- equal --><br/> //等於 <h3>EL執行關系運算</h3> //返回的是boolean類型的 ${ n1<n2 && n3 < n4 } - ${ n1<n2 and n3 < n4 }<br/> //與 ${ n1<n2 || n3 < n4 } - ${ n1<n2 or n3 < n4 }<br/> //或 ${ !(n1 < n2) } - ${ not(n1<n2) } //非 <h3>EL執行三元運算</h3> ${ n1 < n2 ? "正確":"錯誤" } <h3>empty運算</h3> ${ user == null } - ${ empty user } ${ user != null } - ${ not empty user }

【EL操作WEB開發的常用對象11個】

<h1>EL功能三:操作WEB開發常用的對象</h1>
<!-- 
    pageScope,requestScope,sessionScope,applicationScope - 獲取JSP中域中的數據
    param,paramValues     - 接收參數.  接收的是瀏覽器上的參數
    header,headerValues - 獲取請求頭信息
    initParam            - 獲取全局初始化參數
    cookie                - WEB開發中cookie
    pageContext            - WEB開發中的pageContext.
 -->
<h3>接收請求的參數</h3>
//request獲取的是瀏覽器上寫的id 和 name 這是傳統的獲得request的方法 <%= request.getParameter("id") %> <%= request.getParameter("name") %> <%= Arrays.toString(request.getParameterValues("hobby")) %> // <hr/> //使用EL表達式來接收瀏覽器傳來的id 和 name 和hobby參數
${ param.id } ${ param.name } //這是接收單個的值 ${ paramValues.hobby[
0] } //這是接收數組中的值 ${ paramValues.hobby[1] } <h3>獲取請求頭</h3> <%= request.getHeader("User-Agent") %> //傳統獲得瀏覽器的類型方式 <hr/> ${ header["User-Agent"] } //使用EL表達式獲得瀏覽器的類型 <h3>獲取全局初始化參數</h3> 需要在web.xml文件中配置
<context-param>
  <param-name>username</param-name>
  <param-value>root</param-value>
</context-param>
  //用EL表達式獲得全局定義的參數
${ initParam.username }
<h3>獲取Cookie中的值</h3>   //使用EL表達式來獲取cookie的值
${ cookie.history.value } //傳統的太麻煩直接用EL表達是獲取cookie的值 history是cookie的名稱,value是cookie的值
<h3>獲取PageContext中的對象</h3>   pageContext可以獲取其他的8個內置對象
IP地址:${ pageContext.request.remoteAddr } 工程路徑:${ pageContext.request.contextPath } //獲取工程路徑 用的非常的多 只要有.的肯定是有get和set方法的


小編可能技術並不是那麽硬氣,歡迎各位大牛提出寶貴的意見和建議,也希望發現錯誤的朋友指出,在此小編向您表示衷心的感謝。

EL表達式總結