如何使变量可用于表达式求值程序范围?如何添加我自己的?

emp*_*e29 2 java jsp el

我正在尝试了解有关JSP表达式评估的更多信息.如何以及哪些变量被确定为表达式访问的范围?

我有一个简单的JSP(ELEvaluator是一个方便的包装器)

<% String foo = "bar"; %>
<%= ELEvaluator.evaluate("${foo}", request, pageContext) %> //-> blank
<%= ELEvaluator.evaluate("${1 + 1}", request, pageContext %> // -> 2
Run Code Online (Sandbox Code Playgroud)

这些vars/objs是否必须存在于请求obj上?或者在pageContext/Evaluator中注册?

谢谢

JB *_*zet 6

${foo} 手段

  • 找到名为"foo"的页面范围属性,如果找不到
  • 找到名为"foo"的请求范围属性,如果找不到
  • 找到名为"foo"的会话范围属性,如果找不到
  • 找到名为"foo"的应用程序范围属性

因此,如果您希望某些对象可供EL使用,请将其存储在适当的范围内:

pageContext.setAttribute("foo", theObject);
// or
request.setAttribute("foo", theObject);
// or
session.setAttribute("foo", theObject);
// or
servletContext.setAttribute("foo", theObject);
Run Code Online (Sandbox Code Playgroud)