Struts 标签中的 # 、 % 和 $ 符号有什么区别? [英] What's the difference between # , % and $ signs in Struts tags?

查看:27
本文介绍了Struts 标签中的 # 、 % 和 $ 符号有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Struts 2,当我访问 ValueStack 变量时,我不知道是使用 % 还是 #$.我会尝试所有方法,直到找到正确的为止.

I'm working with Struts 2 and when I'm accessing ValueStack variables I don't know whether to use % or # or $. I try all of them until I find the correct one.

谁能解释一下它们之间有什么区别?

推荐答案

#(井号)的使用

OGNL 用于引用 ActionContext 中的对象,如下所示:

OGNL is used to refer to objects in the ActionContext as follows:

  • objectName:ValueStack 中的对象(OGNL 上下文中的默认/根对象),例如 Action 属性
  • #objectName:ActionContext 中但 ValueStack 之外的对象,特别是...
    • #objectName:使用具有默认操作范围的 Struts2 数据标签创建的 ActionContext 对象(例如,<s:set name="foo" value="'Testing'"/>,由 <s:property value="#foo"/>) 引用
    • #parameters.objectName:请求参数
    • #request.objectName:请求范围的属性
    • #session.objectName:会话范围的属性
    • #application.objectName:应用范围的属性
    • #attr.objectName:页面、请求、会话或应用程序范围中的属性(按该顺序搜索)
    • objectName: object in the ValueStack (default/root object in the OGNL context), such as an Action property
    • #objectName: object in the ActionContext but outside of the ValueStack, specifically...
      • #objectName: ActionContext object that has been created using the Struts2 data tags with the default action scope (e.g., <s:set name="foo" value="'Testing'" />, referenced by <s:property value="#foo" />)
      • #parameters.objectName: request parameter
      • #request.objectName: request-scoped attribute
      • #session.objectName: session-scoped attribute
      • #application.objectName: application-scoped attribute
      • #attr.objectName: attribute in page, request, session, or application scope (searched in that order)

      上面的范围映射引用(参数、请求、会话和应用程序)可以通过以下两种方式之一进行:

      The scoped map references above (parameters, request, session, and application) can be made one of two ways:

      • #scopeName.objectName
      • #scopeName['objectName']

      %(百分号)的使用

      %{ OGNL expression } 用于强制对通常被解释为字符串文字的属性进行 OGNL 评估.

      %{ OGNL expression } is used to force OGNL evaluation of an attribute that would normally be interpreted as a String literal.

      示例:<s:property value="myProperty" default="%{myDynamicDefaultValue}"/>

      @的使用(at 符号)

      @ 符号用于引用静态属性和方法.请注意,您可能需要在 Struts2 属性中启用此功能:struts.ognl.allowStaticMethodAccess=true

      The @ symbol is used to make references to static properties and methods. Note that you may need to enable this in your Struts2 properties: struts.ognl.allowStaticMethodAccess=true

      示例:

      @my.package.ClassName@MY_STATIC_PROPERTY
      @my.package.ClassName@myStaticMethod
      

      $(美元符号)的使用

      Struts2 OGNL 没有特别使用美元符号.但是,它可以用于评估普通的 JSTL 表达式.例如:

      Struts2 OGNL does not make special use of the dollar sign. However, it can be used to evaluate normal JSTL expressions. For example:

      Struts2:<h1><s:property value="#pageTitle"/></h1>
      (相当于...)
      JSTL:

      ${pageTitle}

      Struts2: <h1><s:property value="#pageTitle" /></h1>
      (is equivalent to...)
      JSTL: <h1>${pageTitle}</h1>

      这篇关于Struts 标签中的 # 、 % 和 $ 符号有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆