EL表达式
1.EL:expression Language
EL语法很简单,它最大的特点就是使用上很方便,EL的主要语法结构
${sessionScope.user.age}
从Session范围中取得用户的年龄
所有的EL都是以${为起始,以}
为结尾的
2.EL提供.和[]两种运算符来存取数据。如:${sessionScope.user.age}
等同于${sessionScope.user["age"]}
3.EL可以进行自动的类型转换如:${param.score + 11}
4.EL中的隐含对象如:${user.score }
将会从page开始找user直到application中为止。
隐含对象范围:pageScope,requestScope,sessionScope,applicationScope
如:${applicationScope.time}
直接在application范围内
输入参数有关的${param.name}
获取一个请求参数,${paramValues.name}
获取一组请求参数
其他隐含对象:pageContext等(cookie,header,initParam)。
5.EL关系运算符${param.age > 18 ? "adult":"children"}
EL的empty可以作用于一个集合,若该集合不存在或集合中没有元素,其结果都为true
name is empty:${empty requestScope.name}
重点代码示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>El表达式</title>
</head>
<body>
例如:pageContext:<br>
contextPath:${pageContext.request.contextPath}
sessionId:${pageContext.session.id}
sessionAttributeNames:${pageContext.session.attributeNames}
<form action="EL.jsp"method="post">
//用EL表达式可以方便的实现回显
username:<input type="text"name="username"value="${param.username}"/>
<input type="submit"value="Submit"/>
</form>
</body>
</html>