EL 隐式对象
- 隐式(内置)对象就是可以直接使用对象。
- 在之前的文章 EL 与 JSTL 简化 JSP 操作 中介绍了 EL 常用的 2 个隐式对象:pageContext 和 cookie。EL 有 11 个隐式对象,其他的隐式对象又是什么呢?
1. 引入
- 回顾 demo.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>demo.jsp</title>
</head>
<body>
<h3>EL 隐式对象</h3>
${pageContext.request.contextPath} 动态获取:项目名称(虚拟路径)<br>
${cookie.JSESSIONID.value} 获取指定cookie名称的值...<br>
</body>
</html>
- 访问 http://localhost:8080/webappPractice2/demo.jsp,页面显示:
- 在之前获取当前模块的根路径写法是
${pageContext.request.contextPath}
, 为什么不能直接写${requet.contextPath}
呢?
2. 11个隐式对象
- 因为 reqeust 对象不是属于 EL 表达式的内置对象。
3. EL 隐式对象的属性与方法
- 为什么可以获得
pageContext.request
和request.contextPath
呢? - 一个对象是否存在某个属性数据,在 EL 表达式看来只关注 getter 方法。之所以能获得
pageContext.request
和request.contextPath
,是因为 pageContext 和 request 分别有 getRequest 和 getContextPath 这两个方法。具体案例可见下面的应用。
4. 应用:页面跳转
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%--
${pageContext.request.contextPath} 获取当前模块的根目录
--%>
<%-- 方法本质 --%>
<%
request.getContextPath();
pageContext.getRequest();
%>
<a href="${pageContext.request.contextPath}/index.jsp">主页</a>
</body>
</html>
原文链接:https://qwert.blog.csdn.net/article/details/105721081