记录课堂EL的知识点。
Expression Language,EL的全称。
EL有11个内置对象,与JSP九个内置对象一样。
重点的四个:
pageScope:从page范围域属性空间中查找指定的key
requestScope:从request范围域属性空间中查找指定的key
sessionScope:从session范围域属性空间中查找指定的key
applicationScope:从application范围域属性空间中查找指定的key
还有其他重要的对象,比如pageContext,param等等。
其中pageContext.request.contextPath是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的。
案例:
一.
scopes.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% pageContext.setAttribute("username","itcast");%>
<% request.setAttribute("bookname","Java web");%>
<% session.setAttribute("username","itheima");%>
<% application.setAttribute("bookname","java");%>
表达式\${pageScope.username}的值为:${pageScope.username} <br />
表达式\${requestScope.bookname}的值为:${requestScope.bookname} <br />
表达式\${sessionScope.username}的值为:${sessionScope.username} <br />
表达式\${applicationScope.bookname}的值为:${applicationScope.bookname} <br />
表达式\${username}的值为:${username}
</body>
</html>
使用EL表达式来获取四个域的值。
结果:
表达式 p a g e S c o p e . u s e r n a m e 的 值 为 : i t c a s t 表 达 式 {pageScope.username}的值为:itcast 表达式 pageScope.username的值为:itcast表达式{requestScope.bookname}的值为:Java
web 表达式 s e s s i o n S c o p e . u s e r n a m e 的 值 为 : i t h e i m a 表 达 式 {sessionScope.username}的值为:itheima 表达式 sessionScope.username的值为:itheima表达式{applicationScope.bookname}的值为:java 表达式${username}的值为:itcast
二.
param.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body style = "text-align:center;">
<form action = "${pageContext.request.contextPath}/param.jsp">
num1:<input type = "text" name = "num1"><br />
num2:<input type = "text" name = "num"><br />
num3:<input type = "text" name = "num"><br />
<input type = "submit" value = "提交">
<input type = "submit" value = "重置"><hr />
num1: ${param.num1}<br />
num2: ${paramValues.num[0]}<br />
num3: ${paramValues.num[1]}<br />
</form>
</body>
</html>
结果:
num1: 10
num2: 20
num3: 30