EL

概述

EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中脚本的编写。

取值

EL最主要的作用是获得四大域中的数据,格式${EL表达式}
EL获得pageContext域中的值:$(pageContextScope.key);
EL获得request域中的值:$(request.key);
EL获得session域中的值:$(session.key);
EL获得application域中的值:$(application.key);
EL从四个域中获得某个值$(key);
依次从pageContext域,request域,session域,application域中 获取属性,在某个域中获取后将不在向后寻找
示例:

  1. EL获得普通字符串

    1
    2
    3
    4
    5
    <!-- 普通字符串  -->
    <%
    request.setAttribute("str1", "1111111111");
    %>
    ${str1}<br/>
  2. EL获得User对象的值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <!-- User对象 -->
    <%
    //模拟数据,新建一个User对象
    User u = new User();
    u.setAge("22");
    u.setName("user1");
    u.setSalary("5000");
    u.setSex("男");
    //将数据放在request域中
    request.setAttribute("u", u);
    %>
    ${u.name }<br/>
    ${u.age }<br/>
    ${u.sex }<br/>
    ${u.salary }<br/>
  3. EL获得List的值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <!-- List<User> -->
    <%
    //模拟数据,新建一个List<User>
    User u1 = new User();
    u1.setAge("22_1");
    u1.setName("user1_1");
    u1.setSalary("5000_1");
    u1.setSex("男_1");

    User u2 = new User();
    u2.setAge("22_2");
    u2.setName("user1_2");
    u2.setSalary("5000_2");
    u2.setSex("男_2");

    List<User> list1 = new ArrayList();
    list1.add(u1);
    list1.add(u2);
    //将数据放在request域中
    request.setAttribute("u1", list1);
    %>
    ${u1 }<br/>
    ${u1[0] }<br/>
    ${u1[0].name }<br/>
    ${u1[1] }<br/>
    ${u1[1].name }<br/>
  4. EL获得Map<String,String>的值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <!-- Map<String,String> -->
    <%
    Map<String,String> map1 = new HashMap<String,String>();
    map1.put("key1", "value1");
    map1.put("key2", "value2");
    request.setAttribute("map1", map1);
    %>
    ${map1.key1 }<br/>
    ${map1.key2 }<br/>
  5. EL获得Map<String,User>的值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <!-- Map<String,User> -->
    <%
    Map<String,User> map2 = new HashMap<String,User>();
    User u3 = new User();
    u3.setAge("22_3");
    u3.setName("user1_3");
    u3.setSalary("5000_3");
    u3.setSex("男_3");

    User u4 = new User();
    u4.setAge("22_4");
    u4.setName("user1_4");
    u4.setSalary("5000_4");
    u4.setSex("男_4");

    map2.put("u4", u4);
    map2.put("u3", u3);

    request.setAttribute("map2", map2);
    %>
    ${map2.u3 }<br/>
    ${map2.u3.name }<br/>
    ${map2.u3.age }<br/>
    ${map2.u3.sex }<br/>
    ${map2.u3.salary }<br/>
    ${map2.u4 }<br/>
    ${map2.u4.name }<br/>
    ${map2.u4.age }<br/>
    ${map2.u4.sex }<br/>
    ${map2.u4.salary }<br/>

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.rexyan.domain.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>1</title>
</head>
<body>
<!-- 普通字符串 -->
<%
request.setAttribute("str1", "1111111111");
%>
${str1}<br/>

<!-- User对象 -->
<%
//模拟数据,新建一个User对象
User u = new User();
u.setAge("22");
u.setName("user1");
u.setSalary("5000");
u.setSex("男");
//将数据放在request域中
request.setAttribute("u", u);
%>
${u.name }<br/>
${u.age }<br/>
${u.sex }<br/>
${u.salary }<br/>

<!-- List<User> -->
<%
//模拟数据,新建一个List<User>
User u1 = new User();
u1.setAge("22_1");
u1.setName("user1_1");
u1.setSalary("5000_1");
u1.setSex("男_1");

User u2 = new User();
u2.setAge("22_2");
u2.setName("user1_2");
u2.setSalary("5000_2");
u2.setSex("男_2");

List<User> list1 = new ArrayList();
list1.add(u1);
list1.add(u2);
//将数据放在request域中
request.setAttribute("u1", list1);
%>
${u1 }<br/>
${u1[0] }<br/>
${u1[0].name }<br/>
${u1[1] }<br/>
${u1[1].name }<br/>

<!-- Map<String,String> -->
<%
Map<String,String> map1 = new HashMap<String,String>();
map1.put("key1", "value1");
map1.put("key2", "value2");
request.setAttribute("map1", map1);
%>
${map1.key1 }<br/>
${map1.key2 }<br/>

<!-- Map<String,User> -->
<%
Map<String,User> map2 = new HashMap<String,User>();
User u3 = new User();
u3.setAge("22_3");
u3.setName("user1_3");
u3.setSalary("5000_3");
u3.setSex("男_3");

User u4 = new User();
u4.setAge("22_4");
u4.setName("user1_4");
u4.setSalary("5000_4");
u4.setSex("男_4");

map2.put("u4", u4);
map2.put("u3", u3);

request.setAttribute("map2", map2);
%>
${map2.u3 }<br/>
${map2.u3.name }<br/>
${map2.u3.age }<br/>
${map2.u3.sex }<br/>
${map2.u3.salary }<br/>
${map2.u4 }<br/>
${map2.u4.name }<br/>
${map2.u4.age }<br/>
${map2.u4.sex }<br/>
${map2.u4.salary }<br/>
</body>
</html>


=====================打印结果===================
1111111111
user1
22

5000
[User [name=user1_1, sex=男_1, age=22_1, salary=5000_1], User [name=user1_2, sex=男_2, age=22_2, salary=5000_2]]
User [name=user1_1, sex=男_1, age=22_1, salary=5000_1]
user1_1
User [name=user1_2, sex=男_2, age=22_2, salary=5000_2]
user1_2
value1
value2
User [name=user1_3, sex=男_3, age=22_3, salary=5000_3]
user1_3
22_3
男_3
5000_3
User [name=user1_4, sex=男_4, age=22_4, salary=5000_4]
user1_4
22_4
男_4
5000_4

内置对象

el内置对象,是很早以前程序员们大量采用jsp + javabean开发(不需要sevelet)而采用的一种技术,目的是为了拿到会话中的信息,如今这种这种开发模式已经不在,所以只要了解,以及知道该方法在servlet中对应的方法了解即可。

1
2
3
4
5
6
pageScope,requestScope,sessionScope,applicationScope  - 获取JSP中域中的数据
param,paramValues - 接收参数.
header,headerValues - 获取请求头信息
initParam - 获取全局初始化参数
cookie - WEB开发中cookie
pageContext - WEB开发中的pageContext.