#{} ${} 和 %{} 有什么区别?

2022-01-16 00:00:00 variables java jsp struts2

我目前正在使用 struts2,我只是不明白 ${var}#{var} 之间有什么区别>%{var} 它们的作用域不同吗?他们是什么?

I'm currently working with struts2, and I just don't understand what the difference is between ${var}, #{var}, and %{var} are they different scopes? what are they?

我找到了一个#:

<s:select label="Year"
      id="%{param.name}"
      list="#{'2010':'2010','2011':'2011','2012':'2012','2013':'2013','2014':'2014', '2015':'2015'}"
      value="%{currentYear}"
      required="true"
/>

这里看起来像是一个关联数组,但在其他时候我看到它是 #var (不带括号)有什么想法吗?

here it looks like it's an associative array, but there's other times I've seen it as #var (without the brackets) any ideas?

推荐答案

  • ${} - 标准 JSP EL 表示法.
  • #{} - 标准 UEL 表示法;没用过,可以用.
  • %{} - OGNL 表达式表示法.
    • ${} - Standard JSP EL notation.
    • #{} - Standard UEL notation; never used it, may work.
    • %{} - OGNL expression notation.
    • JSP EL 表示法之所以有效,是因为有一个请求包装器,它将首先遵循值堆栈进行查找,然后如果在值堆栈上找不到值,则回退到正常的 JSP 评估.

      JSP EL notation works because there's a request wrapper that will defer to the value stack for lookups first, then fall back to the normal JSP evaluation if there's no value found on the value stack.

      OGNL 表达式符号仅在 S2 标记内有效.每当您评估 OGNL 表达式时,IMO 都应该使用它,尽管它通常是可选的.当它是可选的,然而,这有点废话.通常是,但并非总是如此.最好使用它并明确和交流.

      OGNL expression notation is valid only within S2 tags. IMO it should be used whenever you are evaluating an OGNL expression, although it is quite often optional. When it is optional is somewhat of a crap shoot, however. It often is, buuuut not always. Best to use it and be explicit and communicative.

      您可能询问 # 变量,例如 #session 等.# 用于解析映射"部分中的值堆栈上的值.我将值堆栈视为堆栈和范围的组合:如果已将对象推送到堆栈上,则不需要 #.如果一个值已经创建,你需要#.

      You may be asking about # variables, like #session etc. # is used to resolve a value on the value stack that's in the "map" portion. I view the value stack as a combination stack and scope: if an object has been pushed on the stack, you don't need the #. If a value has been created, you need the #.

      例如,如果您使用 <s:set> 创建变量,则必须使用 # 前缀访问它,例如:

      For example, if you use <s:set> to create a variable, you must access it using a # prefix, like:

      <s:set var="foo" value="'plugh'"/>
      <s:property value="#foo"/>
      

相关文章