EL ExpressionFactory、作用域、JSTL forEach 标签、迭代变量:在哪里?
我正在程序中使用 ExpressionFactory,并且希望使用 EL 表达式从 JSP 访问 ValueExpressions 和变量。
我无法理解一些事情:看起来我的变量覆盖了普通变量,例如,迭代时由 forEach 标记设置的变量。我认为我的代码将用户放入 pageScope 中,显然 forEach 标签也是如此,但我错了。
我应该如何使用工厂和值表达式,以便我可以访问变量,除非它们被覆盖,反之亦然,如下例所示?:
<%@page import="javax.el.ValueExpression"%>
<%@page import="javax.el.ELContext"%>
<%@page import="javax.el.ExpressionFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Check Scopes</title>
</head>
<body>
<%
ExpressionFactory ef = JspFactory.getDefaultFactory().
getJspApplicationContext(application).getExpressionFactory();
ELContext ec = pageContext.getELContext();
ValueExpression ve = ef.createValueExpression(ec, "humangus", String.class);
pageContext.getELContext().getVariableMapper().setVariable("user", ve);
String[]users = new String[]{"mike", "bob", "kate"};
pageContext.setAttribute("users", users);
%>
<h3>List of Users:</h3>
<c:forEach var="user" items="${users}" varStatus="status">
${status.count}:
without specifying scope: ${user},
from page scope: ${pageScope.user}<br/>
</c:forEach>
</body>
</html>
这会产生以下结果:
用户列表:
1:没有指定范围: humangus,来自页面范围: mike
2: 不指定范围: humangus,来自页面范围: bob
3: 不指定范围: humangus,来自页面范围: kate
I am working with ExpressionFactory inside my program, and I wan to make ValueExpressions and variables be accessible from JSP with EL expressions.
I can't understand something: looks like my variables I put way override normal variables, for example, those set by forEach tag when iterating. I am thinking that my code puts user in a pageScope, and obvoiusly so does forEach tag, but I am wrong.
How should I work with factory and value expressions, so that I can access variables unless they are overridden, not vice versa like in the following example?:
<%@page import="javax.el.ValueExpression"%>
<%@page import="javax.el.ELContext"%>
<%@page import="javax.el.ExpressionFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Check Scopes</title>
</head>
<body>
<%
ExpressionFactory ef = JspFactory.getDefaultFactory().
getJspApplicationContext(application).getExpressionFactory();
ELContext ec = pageContext.getELContext();
ValueExpression ve = ef.createValueExpression(ec, "humangus", String.class);
pageContext.getELContext().getVariableMapper().setVariable("user", ve);
String[]users = new String[]{"mike", "bob", "kate"};
pageContext.setAttribute("users", users);
%>
<h3>List of Users:</h3>
<c:forEach var="user" items="${users}" varStatus="status">
${status.count}:
without specifying scope: ${user},
from page scope: ${pageScope.user}<br/>
</c:forEach>
</body>
</html>
This produces the following:
List of Users:
1: without specifying scope: humangus, from page scope: mike
2: without specifying scope: humangus, from page scope: bob
3: without specifying scope: humangus, from page scope: kate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我玩弄了你的代码。
输出:
在 forEach 循环之前用户是 humangus!
1 位用户是 Mike
2 个用户是鲍勃
3 个用户是 kate
在 forEach 循环之后用户是!
???奇怪的是,forEach 循环之后没有找到任何值。
I played around with your code.
Output:
Before forEach loop user is humangus!
1 user is mike
2 user is bob
3 user is kate
After forEach loop user is !
??? Curiously, no value is found after the forEach loop.