1 What is EL?
1.1 EL的作用
JSP2.0要把html和css分离、要把html和javascript分离、要把Java脚本替换成标签。标签的好处是非Java人员都可以使用。
JSP2.0 – 纯标签页面,即:不包含<% … %>、<%! … %>,以及<%= … %>
EL(Expression Language)是一门表达式语言,它对应<%=…%>。我们知道在JSP中,表达式会被输出,所以EL表达式也会被输出。
1.2 EL的格式格式:${…}
例如:${1 + 2}
1.3 关闭EL如果希望整个JSP忽略EL表达式,需要在page指令中指定isELIgnored=”true”。
如果希望忽略某个EL表达式,可以在EL表达式之前添加“\”,例如:\${1 + 2}。
1.4 EL运算符
运算符 | 说明 | 范例 | 结果 |
+ | 加 | ${17+5} | 22 |
- | 减 | ${17-5} | 12 |
* | 乘 | ${17*5} | 85 |
/或div | 除 | ${17/5}或${17 div 5} | 3 |
%或mod | 取余 | ${17%5}或${17 mod 5} | 2 |
==或eq | 等于 | ${5==5}或${5 eq 5} | true |
!=或ne | 不等于 | ${5!=5}或${5 ne 5} | false |
<或lt | 小于 | ${3<5}或${3 lt 5} | true |
>或gt | 大于 | ${3>5}或${3 gt 5} | false |
<=或le | 小于等于 | ${3<=5}或${3 le 5} | true |
>=或ge | 大于等于 | ${3>=5}或${3 ge 5} | false |
&&或and | 并且 | ${true&&false}或${true and false} | false |
!或not | 非 | ${!true}或${not true} | false |
||或or | 或者 | ${true||false}或${true or false} | true |
empty | 是否为空 | ${empty “”},可以判断字符串、数据、集合的长度是否为0,为0返回true。empty还可以与not或!一起使用。${not empty “”} | true |
1.5 EL不显示null
当EL表达式的值为null时,会在页面上显示空白,即什么都不显示。
2 EL表达式格式
- 操作List和数组:${list[0]}、${arr[0]};
- 操作bean的属性:${person.name}、${person[‘name’]},对应person.getName()方法;
- 操作Map的值:${map.key}、${map[‘key’]},对应map.get(key)。
EL一共11个内置对象,无需创建即可以使用。这11个内置对象中有10个是Map类型的,最后一个是pageContext对象。
- pageScope
- requestScope
- sessionScope
- applicationScope
- param;
- paramValues;
- header;
- headerValues;
- initParam;
- cookie;
- pageContext;
域内置对象一共有四个:
- pageScope:${pageScope.name}等同与pageContext.getAttribute(“name”);
- requestScope:${requestScope.name}等同与request.getAttribute(“name”);
- sessionScoep: ${sessionScope.name}等同与session.getAttribute(“name”);
- applicationScope:${applicationScope.name}等同与application.getAttribute(“name”);
如果在域中保存的是JavaBean对象,那么可以使用EL来访问JavaBean属性。因为EL只做读取操作,所以JavaBean一定要提供get方法,而set方法没有要求。
Person.javapackage com.cug.bean01;
public class Person {
private String name;
private int age;
private String sex;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'person.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="p1" class="com.cug.bean01.Person"/>
<jsp:setProperty property="name" name="p1" value="zhu"/>
<jsp:setProperty property="age" name="p1" value="21"/>
<jsp:setProperty property="sex" name="p1" value="男"/>
${pageScope.p1.name }<br/>
${pageScope.p1.age }<br/>
${pageScope.p1.sex }<br/>
</body>
</html>
全域查找:${person}表示依次在pageScope、requesScopet、sessionScope、appliationScope四个域中查找名字为person的属性。
3.2 请求参数相关内置对象
param和paramValues这两个内置对象是用来获取请求参数的。
param:Map<String,String>类型,param对象可以用来获取参数,与request.getParameter()方法相同。
注意,在使用EL获取参数时,如果参数不存在,返回的是空字符串,而不是null。这一点与使用request.getParameter()方法是不同的。
paramValues:paramValues是Map<String, String[]>类型,当一个参数名,对应多个参数值时可以使用它。
header和headerValues是与请求头相关的内置对象:
- header:Map<String,String>类型,用来获取请求头。
- headerValues:headerValues是Map<String,String[]>类型。当一个请求头名称,对应多个值时,使用该对象,这里就不在赘述。
initParam:initParam是Map<String,String>类型。它对应web.xml文件中的<context-param>参数。
3.5 Cookie相关内置对象
cookie:cookie是Map<String,Cookie>类型,其中key是Cookie的名字,而值是Cookie对象本身。
pageContext:pageContext是PageContext类型!可以使用pageContext对象调用getXXX()方法,例如pageContext.getRequest(),可以${pageContext.request}。也就是读取JavaBean属性!!!
EL表达式 | 说明 |
${pageContext.request.queryString} | pageContext.getRequest().getQueryString(); |
${pageContext.request.requestURL} | pageContext.getRequest().getRequestURL(); |
${pageContext.request.contextPath} | pageContext.getRequest().getContextPath(); |
${pageContext.request.method} | pageContext.getRequest().getMethod(); |
${pageContext.request.protocol} | pageContext.getRequest().getProtocol(); |
${pageContext.request.remoteUser} | pageContext.getRequest().getRemoteUser(); |
${pageContext.request.remoteAddr} | pageContext.getRequest().getRemoteAddr(); |
${pageContext.session.new} | pageContext.getSession().isNew(); |
${pageContext.session.id} | pageContext.getSession().getId(); |
${pageContext.servletContext.serverInfo} | pageContext.getServletContext().getServerInfo(); |