EL表達式和EL函數庫(JSTL提供)

EL表達式

1.EL是JSP內置的表達式語言!

  *jsp2.0開始,不讓再使用Java腳本,而是使用el表達式和動做標籤來替代Java腳本!css

  *EL替代的是<%= ....%>,也就是說,EL只能作輸出!html

2.EL表達式來讀取四大域

  * ${xxx},全域查找名爲xxx的屬性,若是不存在,輸出空字符串,而不是null。java

  *  ${pageScope.xxx}、${requestScope.xxx}、${sessionScope.xxx}、${applicationScope.xxx},指定域獲取屬性!web

 1   <body>
 2     <%
 3         pageContext.setAttribute("xxx", "pageContext_XXX");
 4         request.setAttribute("xxx", "request_XXX");
 5         session.setAttribute("xxx", "session_XXX");
 6         application.setAttribute("xxx", "application_XXX");
 7         
 8      %>
 9      
10      ${xxx }<!-- 全域查找,從小到大 --><br>
11      ${pageScope.xxx }<!-- 指定域查找 --><br>
12      ${requestScope.xxx }<!-- 指定域查找 --><br>
13      ${sessionScope.xxx }<!-- 指定域查找 --><br>
14      ${applicationScope.xxx }<!-- 指定域查找 --><br>
15   </body>

請求以後的結果:數組

 

3.JavaBean導航

 Employee.java:--->一個JavaBean瀏覽器

 1 package com.xjs.domain;
 2 
 3 public class Employee {
 4 
 5     private String name;
 6     private double salary;
 7     private Address address;
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public double getSalary() {
15         return salary;
16     }
17     public void setSalary(double salary) {
18         this.salary = salary;
19     }
20     public Address getAddress() {
21         return address;
22     }
23     public void setAddress(Address address) {
24         this.address = address;
25     }
26     @Override
27     public String toString() {
28         return "Employee [name=" + name + ", salary=" + salary + ", address="
29                 + address + "]";
30     }
31 }

Address.java:--->地址的JavaBeancookie

 1 package com.xjs.domain;
 2 
 3 public class Address {
 4 
 5     private String city;
 6     private String street;//街道
 7     public String getCity() {
 8         return city;
 9     }
10     public void setCity(String city) {
11         this.city = city;
12     }
13     public String getStreet() {
14         return street;
15     }
16     public void setStreet(String street) {
17         this.street = street;
18     }
19     @Override
20     public String toString() {
21         return "Address [city=" + city + ", street=" + street + "]";
22     }
23 }

/el/a.jspsession

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.xjs.domain.*" %><!-- 導包 -->
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'a.jsp' starting page</title>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23 
24   </head>
25   
26   <body>
27     <%
28         Address address=new Address();
29         address.setCity("北京");
30         address.setStreet("西三旗");
31         
32         Employee emp=new Employee();
33         emp.setName("金泰妍");
34         emp.setSalary(9999.00);
35         emp.setAddress(address);
36         
37         request.setAttribute("emp", emp);
38      %>
39      <h3>使用el獲取request域的emp</h3>
40      <!-- request.getAttribute("emp").getAddress().getStreet() -->
41      <!-- JavaBean導航 -->
42      ${requestScope.emp.address.street }
43   </body>
44 </html>

請求該頁面結果:app

 

4.EL能夠輸出的東西都在11個內置對象中!

  EL一個11個內置對象,無需建立便可使用。其中10個是Map類型的!pageContext不是Map,它就是pageContext類型,1個頂9個。dom

  *pageScope

  *requestScope

  *sessionScope

  *applicationScope

  *param

  *paramValues;

  *header;

  *headerValues;

  *initParam;

  *cookie;

  *pageContext;

 

  *咱們已經學習了四個。

  *param:對應參數,它是一個Map,其中key屬性名,value是參數值,適用於單值的參數。

1   <body>
2     <!-- map.key這是el的語法!
3     map['key']也能夠操做map
4      -->
5     ${param.username }
6   </body>

在瀏覽器中請求:

  *paramValues:對應參數,它是一個Map,其中key參數名,value是多個參數值,適用於多值的參數。

1   <body>
2     <!-- map.key這是el的語法!
3     map['key']也能夠操做map
4      -->
5     ${param.username }<br>
6     <!-- 數組 -->
7     ${paramValues.hobby[0] }<br>
8     ${paramValues.hobby[1] }
9   </body>

請求:

  *header:對應請求頭,它是一個Map,其中key表示頭名稱,value是單個頭值,適用於單值請求頭。

1   <body>
2     ${header['User-Agent'] }
3   </body>

請求結果:

  *headerValues:對應請求頭,它是一個Map,其中key表示頭名稱,value是多個頭值,適用於多值請求頭。

  *initParam:獲取<context-param>內的參數!---平時不用它

 在web.xml中

1   <context-param>
2       <param-name>xxx</param-name>
3       <param-value>XXX</param-value>
4   </context-param>
5   <context-param>
6       <param-name>yyy</param-name>
7       <param-value>YYY</param-value>
8   </context-param>

${initParam.xxx}

1   <body>
2     ${initParam.xxx }
3   </body>

請求結果:

  *cookie:Map<String,Cookie>類型,其中key是cookie的name,value是cookie對象。使用例如:${cookie.username.value}

1   <body>
2     ${cookie.JSESSIONID.value }
3   </body>

請求結果:

  *pageContext:它是PageContext類型!${pageContext.request.scheme}--->至關於調用getRequest().getScheme();獲取協議。

 

  <body>
      <!-- 項目名 -->
    ${pageContext.request.contextPath }
    
    <hr>
    
    <a href="${pageContext.request.contextPath }/cookie/a.jsp">點擊這裏</a>
    
    <form method="post" action="${pageContext.request.contextPath }/el/a.jsp">
        <input type="submit" value="xxx">
    </form>
  </body>

請求以後的結果:

在以後的表單或超連接中的項目名能夠這樣獲取!!!

 


EL函數庫(由JSTL提供)

  *導入標籤庫:<%@   tablib  prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions " %>

 


自定義函數庫

  *寫一個java類,類中能夠定義0~N個方法,但必須是static ,並且有返回值的!

相關文章
相關標籤/搜索