EL ExpressionFactory、作用域、JSTL forEach 标签、迭代变量:在哪里?

发布于 2024-11-08 06:38:03 字数 1675 浏览 0 评论 0原文

我正在程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-11-15 06:38:04

我玩弄了你的代码。

  <%
     pageContext.setAttribute("user", "humangus");
     String[]users = new String[]{"mike", "bob", "kate"};
     pageContext.setAttribute("users", users);
  %>
Before forEach loop user is ${user}!<br/>
<c:forEach var="user" items="${users}" varStatus="status">
  ${status.count} user is ${user}<br/>
</c:forEach>
After forEach loop user is ${user}!

输出:
在 forEach 循环之前用户是 humangus!
1 位用户是 Mike
2 个用户是鲍勃
3 个用户是 kate
在 forEach 循环之后用户是!

???奇怪的是,forEach 循环之后没有找到任何值。

I played around with your code.

  <%
     pageContext.setAttribute("user", "humangus");
     String[]users = new String[]{"mike", "bob", "kate"};
     pageContext.setAttribute("users", users);
  %>
Before forEach loop user is ${user}!<br/>
<c:forEach var="user" items="${users}" varStatus="status">
  ${status.count} user is ${user}<br/>
</c:forEach>
After forEach loop user is ${user}!

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文