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

问题描述

我正在使用Struts2,当我访问值堆栈变量时,我不知道是否使用%#$.我会尝试所有这些,直到找到正确的.

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

任何人都可以解释一下两者之间的区别吗?

Can Anybody Explain what is the difference between these?

推荐答案

使用#(井号)

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

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

  • objectName:ValueStack中的对象(OGNL上下文中的默认/根对象),例如Action属性
  • #objectName:对象位于ActionContext中,但位于ValueStack之外,尤其是...
    • #objectName:使用Struts2数据标签和默认操作范围(例如,<s:set name="foo" value="'Testing'" />,由<s:property value="#foo" />引用)创建的ActionContext对象
    • #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']
      • #scopeName.objectName or
      • #scopeName['objectName']

      %(百分号)的使用

      %{ OGNL expression }用于强制对通常会解释为String文字的属性进行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}" />

      使用@(符号)

      @符号用于引用静态属性和方法.请注意,您可能需要在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:<h1>${pageTitle}</h1>

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

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

09-21 11:03