**
EL表达式
**
EL表达式可以嵌入到jsp页面的内部,目的就是用来简化jsp页面的书写。
看一下简化后的效果:
jsp脚本:
<%=request.getAttribute(name)%>
EL表达式替代上面的脚本:
${requestScope.name}
下面看一下EL表达式的运算符
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String str="acb";
request.setAttribute("str",str);
//empty运算符可以判断所有有长度的数据类型(字符串,数组,集合)的长度,只有长度大于0时才返回false,null与长度为0都返回true
%>
<%--运算符--%>
<h3>算术运算符</h3>
${3+4}<br>
${3/4}<br>
${3%4}<br>
<h3>比较运算符</h3>
${3<4}<br>
<h3>逻辑运算符</h3>
${3<4||3>4}<br>
<h3>空运算符</h3>
${empty str}<br>
</body>
</html>
看一下页面的效果
EL表达式通过域名称来获取值:
下面是域名称(前者)对应的域对象(后者)
pageScope ----> pageContext
requestScope ----> request
sessionScope ----> session
applicationScope ----> application
以上的四个域对象所代表的范围由上到下依次增加
我们来看一下EL表达式获取对象的两种方式:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("name","gfx");
session.setAttribute("age","20");
%>
<h3>EL获取值第一种方式</h3>
${requestScope.name}<br>
${sessionScope.age}<br>
<h3>EL获取值第二种方式</h3>
${name}<br>
</body>
</html>
对于第二种获取方式,会从最小的域中开始找,直到找到。
下面是EL表达式获取值的一些操作
<%@ page import="domain.User" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
User user=new User();
user.setName("AJ");
user.setAge(20);
user.setBirthday(new Date());
request.setAttribute("user",user);
List list=new ArrayList();
list.add("AAA");
list.add("BBB");
list.add("CCC");
request.setAttribute("list",list);
Map map=new HashMap();
map.put("gender","男");
map.put("power","hight");
request.setAttribute("map",map);
%>
<%--El获取对象的值是通过属性来获取的:
setter或getter方法,去掉set与get,将剩余部分首字母小写
如setName——>Name——>name
--%>
<h3>EL获取对象的值</h3>
${requestScope.user.name}<br>
${requestScope.user.age}<br>
${requestScope.user.birthday}<br>
<h3>EL获取List集合的值</h3>
${requestScope.list}<br>
${requestScope.list[0]}<br>
${requestScope.list[1]}<br>
<h3>EL获取Map集合的值</h3>
${requestScope.map.gender}<br>
<h3>EL的隐式对象pageContext</h3>
${pageContext.request.contextPath}<br>
</body>
</html>
页面: