1、EL 表达式
1.1、EL 表达式的作用
- EL 表达式的全称是:Expression Language。是表达式语言。
- EL 表达式的什么作用:EL 表达式主要是代替jsp 页面中的表达式脚本在jsp 页面中进行数据的输出。
- 因为EL 表达式在输出数据的时候,要比jsp 的表达式脚本要简洁很多。
<%
request.setAttribute("key","值");
%>
表达式脚本输出key 的值是:
<%=request.getAttribute("key1")==null?"":request.getAttribute("key1")%>
EL 表达式输出key 的值是:${key1}
- EL 表达式的格式是:
${表达式}
- EL 表达式在输出
null
值的时候,输出的是空串。
- jsp 表达式脚本输出
null
值的时候,输出的是null
字符串。
1.2、EL 表达式搜索域数据的顺序
- EL 表达式主要是在jsp 页面中输出数据。
- 主要是输出域对象中的数据。
- 当四个域中都有相同的key 的数据的时候,EL 表达式会按照四个域的从小到大的顺序去进行搜索,找到就输出。
<%
//往四个域中都保存了相同的key 的数据。
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
pageContext.setAttribute("key", "pageContext");
%>
${ key }
1.3、EL 表达式输出Bean
public class Person {
private String name;
private String[] phones;
private List<String> cities;
private Map<String,Object> map;
public int getAge() {
return 18;
}
}
<%
Person person = new Person();
person.setName("国哥");
person.setPhones(new String[]{"18610541354","18688886666","18699998888"});
List cities = new ArrayList();
cities.add("北京");
cities.add("上海");
cities.add("深圳");
person.setCities(cities);
Mapmap = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
person.setMap(map);
pageContext.setAttribute("p", person);
%>
输出Person:${ p }
输出Person 的name 属性:${p.name}
输出Person 的pnones 数组属性值:${p.phones[2]}
输出Person 的cities 集合中的元素值:${p.cities}
输出Person 的List 集合中个别元素值:${p.cities[2]}
输出Person 的Map 集合: ${p.map}
输出Person 的Map 集合中某个key 的值: ${p.map.key3}
输出Person 的age 属性:${p.age}
1.4、EL 表达式——运算
- 语法:
${ 运算表达式}
- EL 表达式支持如下运算符:
关系运算
关系运算符 |
说明 |
范例 |
结果 |
== 或 eq |
等于 |
5 = = 5 或 { 5 == 5 } 或 5==5或{ 5 eq 5 } |
true |
!= 或 ne |
不等于 |
5 ! = 5 或 { 5 !=5 } 或 5!=5或{ 5 ne 5 } |
false |
< 或 lt |
小于 |
3 < 5 或 { 3 < 5 } 或 3<5或{ 3 lt 5 } |
true |
> 或gt |
大于 |
2 > 10 或 { 2 > 10 } 或 2>10或{ 2 gt 10 } |
false |
<= 或 le |
小于等于 |
5 < = 12 或 { 5 <= 12 } 或 5<=12或{ 5 le 12 } |
true |
>= 或 ge |
大于等于 |
3 > = 5 或 { 3 >= 5 } 或 3>=5或{ 3 ge 5 } |
false |
逻辑运算
逻辑运算符 |
说明 |
范例 |
结果 |
&& 或and |
与运算 |
KaTeX parse error: Expected '}', got '&' at position 12: { 12 == 12 &̲& 12 < 11 } 或{ 12 == 12 and 12 < 11 } |
false |
|| 或 or |
或运算 |
12 = = 12 ∥ ∥ 12 < 11 或 { 12 == 12 \|\| 12 < 11 } 或 12==12∥∥12<11或{ 12 == 12 or 12 < 11 } |
true |
! 或 not |
取反运算 |
! t r u e 或 { !true } 或 !true或{not true } |
false |
算数运算
算数运算符 |
说明 |
范例 |
结果 |
+ |
加法 |
${ 12 + 18 } |
30 |
- |
减法 |
${ 18 - 8 } |
10 |
* |
乘法 |
${ 12 * 12 } |
144 |
/ 或 div |
除法 |
144 / 12 或 { 144 / 12 } 或 144/12或{ 144 div 12 } |
12 |
% 或 mod |
取模 |
KaTeX parse error: Expected '}', got 'EOF' at end of input: { 144 % 10 } 或{ 144 mod 10 } |
4 |
empty 运算
- empty 运算可以判断一个数据是否为空,如果为空,则输出true,不为空输出false。
- 以下几种情况为空:
- 值为null 值的时候,为空
- 值为空串的时候,为空
- 值是Object 类型数组,长度为零的时候
- list 集合,元素个数为零
- map 集合,元素个数为零
<%
// 1、值为null 值的时候,为空
request.setAttribute("emptyNull", null);
// 2、值为空串的时候,为空
request.setAttribute("emptyStr", "");
// 3、值是Object 类型数组,长度为零的时候
request.setAttribute("emptyArr", new Object[]{});
// 4、list 集合,元素个数为零
List list = new ArrayList<>();
// list.add("abc");
request.setAttribute("emptyList", list);
// 5、map 集合,元素个数为零
Map map = new HashMap();
// map.put("key1", "value1");
request.setAttribute("emptyMap", map);
%>
${ empty emptyNull }
${ empty emptyStr }
${ empty emptyArr }
${ empty emptyList }
${ empty emptyMap }
三元运算
表达式1?表达式2:表达式3
- 如果表达式1 的值为真,返回表达式2 的值,如果表达式1 的值为假,返回表达式3 的值。
“.”点运算和[ ]中括号运算符
- .点运算,可以输出Bean 对象中某个属性的值。
- [ ]中括号运算,可以输出有序集合中某个元素的值。
- 并且[ ]中括号运算,还可以输出map 集合中key 里含有特殊字符的key 的值。
<%
Map map = new HashMap();
map.put("a.a.a", "aaaValue");
map.put("b+b+b", "bbbValue");
map.put("c-c-c", "cccValue");
request.setAttribute("map", map);
%>
${ map['a.a.a'] }
${ map["b+b+b"] }
${ map['c-c-c'] }
1.5、EL 表达式的11 个隐含对象
EL 个达式中11 个隐含对象,是EL 表达式中自己定义的,可以直接使用。
变量 |
类型 |
作用 |
pageContext |
PageContextImpl |
它可以获取jsp中的九大内置对象 |
pageScope |
Map |
它可以获取pageContext域中的数据 |
requestScope |
Map |
它可以获取Request域中的数据 |
sessionScope |
Map |
它可以获取Session 域中的数据 |
applicationScope |
Map |
它可以获取ServletContext 域中的数据 |
param |
Map |
它可以获取请求参数的值 |
paramValues |
Map |
它也可以获取请求参数的值,获取多个值的时候使用 |
header |
Map |
它可以获取请求头的信息 |
headerValues |
Map |
它可以获取请求头的信息,它可以获取多个值的情况 |
cookie |
Map |
它可以获取当前请求的Cookie 信息 |
initParam |
Map |
它可以获取在web.xml中配置的 上下文参数 |
EL 获取四个特定域中的属性
- pageScope ====== pageContext 域
- requestScope ====== Request 域
- sessionScope ====== Session 域
- applicationScope ====== ServletContext 域
<%
pageContext.setAttribute("key1", "pageContext1");
pageContext.setAttribute("key2", "pageContext2");
request.setAttribute("key2", "request");
session.setAttribute("key2", "session");
application.setAttribute("key2", "application");
%>
${ applicationScope.key2 }
pageContext 对象的使用
- 协议:
request.getScheme()
- 服务器ip:
request.getServerName()
- 服务器端口:
request.getServerPort()
- 获取工程路径:
getContextPath()
- 获取请求方法:
request.getMethod()
- 获取客户端ip 地址:
request.getRemoteHost()
- 获取会话的id 编号:
session.getId()
<%
pageContext.setAttribute("req", request);
%>
<%=request.getScheme() %>
1.协议: ${ req.scheme }
2.服务器ip:${ pageContext.request.serverName }
3.服务器端口:${ pageContext.request.serverPort }
4.获取工程路径:${ pageContext.request.contextPath }
5.获取请求方法:${ pageContext.request.method }
6.获取客户端ip 地址:${ pageContext.request.remoteHost }
7.获取会话的id 编号:${ pageContext.session.id }
2、JSTL 标签库
- JSTL 标签库全称是指JSP Standard Tag Library JSP 标准标签库。是一个不断完善的开放源代码的JSP 标签库。
- EL 表达式主要是为了替换jsp 中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个jsp 页面变得更佳简洁。
- JSTL 由五个不同功能的标签库组成
功能范围 |
URI |
前缀 |
核心标签库(重点) |
http://java.sun.com/jsp/jstl/core |
c |
格式化 |
http://java.sun.com/jsp/jstl/fmt |
fmt |
函数 |
http://java.sun.com/jsp/jstl/functions |
fn |
数据库(不使用) |
http://java.sun.com/jsp/jstl/sql |
sql |
XML(不使用) |
http://java.sun.com/jsp/jstl/xml |
x |
- 在jsp 标签库中使用taglib 指令引入标签库
<%@ taglib prefix="前缀" uri="URI" %>
2.1、JSTL 标签库的使用步骤
- 导入jstl 标签库的jar 包。
- taglibs-standard-impl-1.2.1.jar
- taglibs-standard-spec-1.2.1.jar
- 使用taglib 指令引入标签库。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2.2、core 核心库使用
(使用很少)
<%--
i.
作用:set 标签可以往域中保存数据
域对象.setAttribute(key,value);
scope 属性设置保存到哪个域
page 表示PageContext 域(默认值)
request 表示Request 域
session 表示Session 域
application 表示ServletContext 域
var 属性设置key 是多少
value 属性设置值
--%>
保存之前:${ sessionScope.abc }
保存之后:${ sessionScope.abc }
<%--
ii.
if 标签用来做if 判断。
test 属性表示判断的条件(使用EL 表达式输出)
--%>
12 等于12
12 不等于12
标签
- 作用:多路判断。跟switch … case … default 非常接近
<%--
iii. 标签
作用:多路判断。跟switch ... case .... default 非常接近
choose 标签开始选择判断
when 标签表示每一种判断情况
test 属性表示当前这种判断情况的值
otherwise 标签表示剩下的情况
标签使用时需要注意的点:
1、标签里不能使用html 注释,要使用jsp 注释
2、when 标签的父标签一定要是choose 标签
--%>
<%
request.setAttribute("height", 180);
%>
<%-- 这是html 注释--%>
小巨人
很高
还可以
大于160
大于150
大于140
其他小于140
遍历1到10输出
<%--1.遍历1 到10,输出
begin 属性设置开始的索引
end 属性设置结束的索引
var 属性表示循环的变量(也是当前正在遍历到的数据)
for (int i = 1; i < 10; i++)
--%>
遍历Object数组
<%-- 2.遍历Object 数组
for (Object item: arr)
items 表示遍历的数据源(遍历的集合)
var 表示当前遍历到的数据
--%>
<%
request.setAttribute("arr", new String[]{"18610541354","18688886666","18699998888"});
%>
${ item }
遍历Map集合
<%
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
// for ( Map.Entry entry : map.entrySet()) {
// }
request.setAttribute("map", map);
%>
${entry.key} = ${entry.value}
遍历List集合
public class Student {
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
}
<%--4.遍历List 集合---list 中存放Student 类,有属性:编号,用户名,密码,年龄,电话信息--%>
<%
List studentList = new ArrayList();
for (int i = 1; i <= 10; i++) {
studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i));
}
request.setAttribute("stus", studentList);
%>
编号 |
用户名 |
密码 |
年龄 |
电话 |
操作 |
<%--
items 表示遍历的集合
var 表示遍历到的数据
begin 表示遍历的开始索引值
end 表示结束的索引值
step 属性表示遍历的步长值
varStatus 属性表示当前遍历到的数据的状态
for(int i = 1; i < 10; i+=2)
--%>
${stu.id} |
${stu.username} |
${stu.password} |
${stu.age} |
${stu.phone} |
${status.step} |