天天看點

表達式語言EL

EL,表達式語言,用于代替JSP中的Java腳本段。

表達式語言EL

表達式語言

  EL(Expression Language,表達式語言)主要是用在JSP頁面中,用來輔助我們産生無腳本的JSP頁面,此處的腳本指的是JSP中的Java代碼。

  EL的文法是這樣的:

${expr}      

  如果你想在頁面上直接輸出上面的内容,需要進行轉義,加上反斜杠即可:

\${expr}      

  再比如:

${sessionScope.user.sex}      

  所有EL都是以 ${ 為起始、以} 為結尾的。

  Expressions can be used in two ways:

  1.Attribute values in custom and standard actions

  2.Within template text

EL可以用來通路Java Bean

  Beans within the namespace available to the JSP can be accessed easily using EL.

  1.Beans can be accessed by way of dot notation:

  ${bean.attribute}

  2.Beans can be located by searching through the scopes:

  page, request, session and application.

  3.Bean scope can be specified by preceding the bean name with the scope.

  ${sessionScope.cust.firstName}

執行個體1

  JSP頁面el1.jsp裡有一個表單:

<body>
    <form action="el/el2.jsp">
    username: <input type="text" name="username">
    <input type="submit" value="submit">
    </form>
  </body>      

  在頁面el2.jsp裡面擷取這個參數顯示:

  之前可以:

<%= request.getParameter("username") %>      

  現在可以:

${ param.username }      

  兩種效果是一樣的。唯一的不同是使用EL打出點之後即便Alt+/也沒有任何的輸入提示。

執行個體頁面

  也可以顯示Session中的屬性值:

表達式語言EL
表達式語言EL
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'el1.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="el/el2.jsp">
    username: <input type="text" name="username"><br>   
    interest: <input type="text" name="interest"><br> 
    interest: <input type="text" name="interest"><br> 
    interest: <input type="text" name="interest"><br>
    interest: <input type="text" name="interest"><br>
    interest: <input type="text" name="interest"><br> 
    <input type="submit" value="submit">
    </form>
    
    <% session.setAttribute("hello", "world");%>
  </body>
</html>      

el1.jsp

表達式語言EL
表達式語言EL
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'el2.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>

    get parameter user name:
    <br>
    <%=request.getParameter("username")%>
    <br> 
    ${ param.username }
    <br>
    <br> 
    get session attribute:
    <br>
    <%=session.getAttribute("hello")%>
    <br>
    ${ sessionScope.hello }
    
    <br>
    get param in an array:
    <br>
    ${ paramValues.interest[3] }
    
    <br>
    calculation:
    <br>
    ${ 3/4 }
    <br>
    ${3 div 5}
</body>
</html>      

el2.jsp

EL預設對象

表達式語言EL
表達式語言EL

運算符

  EL表達式支援:

5個算術運算符:

  加(+)減(-)乘(*)除(/ and div)和取模(% and mod)。

6個比較操作符:

表達式語言EL

3個邏輯運算符:

表達式語言EL

.和[]運算符

  EL 提供 . 和 [ ] 兩種運算符來存取資料。下列兩者所代表的意思是一樣的

  ${sessionScope.user.sex} 等于

  ${sessionScope.user["sex"]}

  . 和 [ ] 也可以同時混合使用,如下:

  ${sessionScope.shoppingCart[0].price}

  回傳結果為shoppingCart中第一項物品的價格。

兩者差異:

差異1:特殊字元需要包含在[]裡。

  當要存取的屬性名稱中包含一些特殊字元,如 . 或 – 等并非字母或數字的符号,就一定要使用 [ ],例如:

  ${user.My-Name }

  上述是不正确的方式,應當改為:

  ${user["My-Name"] }

差異2:[]中可以放置變量,進行動态取值。

  ${sessionScope.user[data]}

  此時,data 是一個變量。

  假若data的值為"sex"時,那上述的例子等于${sessionScope.user.sex};

  假若data 的值為"name"時,它就等于${sessionScope.user.name}。

  是以,如果要動态取值時,就可以用上述的方法來做,但 . 無法做到動态取值。

EL變量

  EL 存取變量資料的方法很簡單,例如:${username}。

  它的意思是取出某一範圍中名稱為username的變量。

  因為我們并沒有指定哪一個範圍的username,是以它的預設值會先從Page 範圍找,假如找不到,再依序到Request、Session、Application範圍。

  假如途中找到username,就直接回傳,不再繼續找下去,但是假如全部的範圍都沒有找到時,就回傳null。

  我們也可以指定要取出哪一個範圍的變量。如:

表達式語言EL

pageContext

  可以使用${pageContext}來取得其他有關使用者要求或頁面的詳細資訊:

表達式語言EL
表達式語言EL

參考資料

  聖思園張龍老師Java Web視訊教程。

作者: 聖騎士Wind

出處: 部落格園: 聖騎士Wind

Github: https://github.com/mengdd

微信公衆号: 聖騎士Wind

表達式語言EL