1、EL表达式的基本概述
EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中输出脚本<%= 数据 %>的编写。
2、EL表达式的格式和作用
EL表达式的格式:${有效的EL表达式}
EL表达式的作用:
●EL提供了一些标识符、存取器和运算符,用来读取驻留在Web容器中的数据。
●EL可用于JSP页面中所有HTML和JSP标签中。
●EL表达式只能使用在除了JSP脚本(jsp表达式<%=%>,jsp代码片段<% %>)的地方
3、EL表达式的基本使用
3.1、EL表达式中的常量
文字常量被指定为固定值,主要包括:
●数字:整数、浮点数
●字符串:任何由单引号或双引号限定的字符串
●布尔型:true、false
●空值:null输出时会转换成空串显示
●但是JSP的表达式在输出结果为null时,页面就显示null.
3.2、EL表达式中的字符串
类别 | 运算符 |
---|---|
算术运算符 | + 不能用字符串的连接 - * / (或div) % (或mod) |
关系运算符 | == (或eq) 可用于判断字符串内容是否相等 != (或ne) < (或lt) > (或gt) <= (或le) >= (或ge) |
逻辑运算符 | && (或and) ll (或or) !(或 not) |
条件运算符 | booleanExpt ? result1 : result2 |
验证空值 | empty 用来验证是否为null、空字符串、空数组、空集合 |
提高优先级 | () |
常量和运算符代码展示:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/10/22
Time: 10:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL常量和运算符</title>
</head>
<body>
<%
pageContext.setAttribute("mas", "context");
%>
<h3>EL表达式中的常量</h3>
${'lisi'} ${100} ${false} ${"lisi"} ${null}
<h3>EL表达式中的运算符</h3>
<h5>算数运算符</h5>
<p>10+10:${10+10}</p>
<p>10-10:${10-10}</p>
<p>10*10:${10*10}</p>
<p>10/10:${10/10}</p>
<p>10%10:${10%10}</p>
<h5>关系运算符</h5>
<p>${"zhangsan"=="zhangsan"}</p>
<h5>逻辑运算符</h5>
<p>${5>2 and 3<10}</p>
<p>${5>2 or 3>10}</p>
<p>${!(2>5)}</p>
<h5>条件运算符</h5>
<p>${5>2?"shi":"fou"}</p>
<h5>empty运算符</h5><%--判断--%>
<p>${empty mas}</p>
</body>
</html>
3.3、EL中的存取器
EL提供了两种不同的存取器来存取对象的属性值或集合中的元素。
●点运算符:
●方括号运算符:[]
代码演示:
<%@ page import="com.bdit.Student" %>
<%@ page import="java.util.*" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/10/22
Time: 10:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el存取器</title>
</head>
<body>
<%
Student stu=new Student("lisi",18);
pageContext.setAttribute("a",stu);
List<String> list=new ArrayList<>();
list.add("lisi");
list.add("zhangsan");
list.add("xiaoming");
pageContext.setAttribute("list",list);
List<Student> list2=new ArrayList<>();
list2.add(new Student("xiaoming",16));
list2.add(new Student("xiaohong",56));
pageContext.setAttribute("list2",list2);
Set<String> set=new HashSet<>();
set.add("shanghai");
set.add("beijing");
set.add("heze");
pageContext.setAttribute("set",set);
Map<String,Student> map=new HashMap<>();
map.put("a",new Student("lisi",15));
map.put("b",new Student("zhangsan",65));
pageContext.setAttribute("map",map);
%>
<h5>获取javabean中的属性</h5>
<P>name:${a.name}</P>
<P>age:${a.age}</P>
<h5>获取list集合中的元素</h5>
<p>${list[0]}</p>
<p>${list[1]}</p>
<p>${list[2]}</p>
<h5>获取list集合中的javabean元素</h5>
<p>${list2[1].name}</p>
<p>${list2[1].age}</p>
<h5>获取set集合中的元素</h5>
<p>${set.toArray()[0]}</p>
<p>${set.toArray()[1]}</p>
<p>${set.toArray()[2]}</p>
<h5>获取map集合中的元素</h5>
<p>${map['a'].name}</p>
<p>${map['a'].age}</p>
<p>${map.keySet()}</p>
<p>${map.values()}</p>
</body>
</html>
4、EL中的隐式对象
EL中有11个隐式对象变量,这些变量可以直接用来访问特定的数据。
在EL中,与隐式对象变量无关联的变量被认为是存储在四个JSP作用域(page,request,session,application)中的对象。
作用域中对象的搜索顺序是page、request、session、application。
代码演示:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/10/22
Time: 11:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL的隐式对象</title>
</head>
<body>
<h3>四个作用域</h3>
<%
pageContext.setAttribute("a","lisi");
request.setAttribute("a","zhangsan");
session.setAttribute("a","zhaoliu");
application.setAttribute("a","tianqi");
%>
<p>页面作用域:${pageScope.a}</p>
<p>请求作用域:${requestScope.a}</p>
<p>会话作用域:${sessionScope.a}</p>
<p>上下文作用域:${applicationScope.a}</p>
<h4>不和el隐式对象关联</h4>
<p>param:${param.name}</p><!--获取单选框的值-->
<!--获取多选框的值-->
<p>paramValues:${paramValues.hobby[0]} ${paramValues.hobby[1]} ${paramValues.hobby[2]}</p>
<p>pageContext:${pageContext.request.contextPath}</p>
<p>cookie:${cookie.JSESSIONID.value}</p>
<p>initParam:${initParam.version}</p><!--web.xml里面的初始化参数-->
</body>
</html>
5、禁用EL和EL关键字
禁用EL表达式
禁用EL的语法为:
<%@pageisELIgnored="true|false"%>
true:表示忽略对EL表达式进行计算;
false:表示不忽略。
isELIgnored的默认值为false
EL中的关键字
在程序中不要使用EL的关键字作为变量名或方法的名称,否则无法用EL来存取。