EL(Expression Language) 是为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化。
下面是部分功能测试代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
import="com.jsp.study.bean.User"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'testEL.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>
<%
List<User> userList = new ArrayList<User>();
userList.add(new User(0, "李x"));
userList.add(new User(1, "贺x"));
request.setAttribute("userList", userList);
//session.setAttribute("userList", userList);
pageContext.setAttribute("name", "pageName");
request.setAttribute("name", "requestName");
application.setAttribute("name", "applicationName");
pageContext.setAttribute("userName", "pageName");
request.setAttribute("userName", "requestName");
session.setAttribute("userName", "sessionName");
application.setAttribute("userName", "applicationName");
%>
<body>
<!-- 依次从page、request、session、application四大域中取值 -->
<h2>获取用户列表集合{userList}:${userList}</h2>
<h2>获取用户列表第一条记录{userList[0].name}:${userList[0].name}</h2>
<h2>返回的用户名称{name}:${name}</h2>
<!-- 利用jsp上下文对象获取其他八大对象,然后操作域内数据 -->
<h2>获取{pageContext.request.contextPath}:${pageContext.request.contextPath}</h2>
<h2>获取用户列表集合{pageContext.request.serverPort}:${pageContext.request.serverPort}</h2>
<!-- 四大域取值方法 -->
<h2>获取{pageScope.userName}:${pageScope.userName}</h2>
<h2>获取{requestScope.userName}:${requestScope.userName}</h2>
<h2>获取{sessionScope.userName}:${sessionScope.userName}</h2>
<h2>获取{applicationScope.userName}:${applicationScope.userName}</h2>
<!-- EL也可以用来运算 -->
<h2>判断userList是否为空:${userList==null}</h2>
<h2>算法表达式:${1+1}</h2>
<!-- 还有其他功能,这里就不测试了,用到的时候查看相关资料... -->
</body>
</html>
总结:
EL就是一种获取不同域内数据,使jsp页面代码编码更方便简洁的一种语法工具。