Struts 标签中的 # 、 % 和 $ 符号有什么区别?

发布于 2024-12-14 05:00:25 字数 183 浏览 1 评论 0 原文

我正在使用 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.

Can anybody explain what is the difference between them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

生生漫 2024-12-21 05:00:25

使用#(井号)

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

  • objectName:ValueStack 中的对象(OGNL 上下文中的默认/根对象) ,例如 Action 属性
  • #objectName:ActionContext 中但在 ValueStack 之外的对象,具体来说...
    • #objectName:使用具有默认操作范围的 Struts2 数据标记创建的 ActionContext 对象(例如,,由 引用)
    • #parameters.objectName:请求参数
    • #request.objectName:请求范围的属性
    • #session.objectName:会话范围属性
    • #application.objectName:应用程序范围的属性
    • #attr.objectName:页面、请求、会话或应用程序范围中的属性(按顺序搜索)

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

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

使用 %(百分号)

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

示例:

@(at 符号)的使用

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

示例:

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

使用 $(美元符号)

Struts2 OGNL 没有特殊用途美元符号。然而,它可以用来计算普通的 JSTL 表达式。例如:

Struts2:


(相当于...)
JSTL:

${pageTitle}

Use of # (pound sign)

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

  • 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 or
  • #scopeName['objectName']

Use of % (percent sign)

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

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

Use of @ (at sign)

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

Examples:

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

Use of $ (dollar sign)

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>
(is equivalent to...)
JSTL: <h1>${pageTitle}</h1>

琴流音 2024-12-21 05:00:25

该框架使用标准命名上下文来计算 OGNL 表达式。处理 OGNL 的顶级对象是 Map(通常称为上下文映射或上下文)。 OGNL 有一个概念,即上下文中存在根(或默认)对象。在表达式中,可以引用根对象的属性,而无需任何特殊的“标记”概念。对其他对象的引用用井号 (#) 标记。

框架将 OGNL 上下文设置为我们的 ActionContext,并将值堆栈设置为 OGNL 根对象。 (值堆栈是多个对象的集合,但对于 OGNL 来说,它似乎是单个对象。)除了值堆栈之外,框架还将其他对象放置在 ActionContext 中,包括表示应用程序、会话和请求上下文的映射。这些对象与值堆栈(我们的 OGNL 根)一起共存于 ActionContext 中。

                 |
                 |--application
                 |
                 |--session
   context map---|
                 |--value stack(root)
                 |
                 |--request
                 |
                 |--parameters
                 |
                 |--attr (searches page, request, session, then application scopes)

请参阅此了解更多详细信息

OGNL 基础

基本上 struts2 将对象值 Stack 放在顶部大多数对象和 OGNL 用于引用它们。根对象可以在没有任何特殊“标记”的情况下或使用 % 进行引用,而对其他对象的引用则用井号 (#) 进行标记。 # 基本上用于引用应用程序/会话等中的对象/值。

The framework uses a standard naming context to evaluate OGNL expressions. The top level object dealing with OGNL is a Map (usually referred as a context map or context). OGNL has a notion of there being a root (or default) object within the context. In expression, the properties of the root object can be referenced without any special "marker" notion. References to other objects are marked with a pound sign (#).

The framework sets the OGNL context to be our ActionContext, and the value stack to be the OGNL root object. (The value stack is a set of several objects, but to OGNL it appears to be a single object.) Along with the value stack, the framework places other objects in the ActionContext, including Maps representing the application, session, and request contexts. These objects coexist in the ActionContext, alongside the value stack (our OGNL root).

                 |
                 |--application
                 |
                 |--session
   context map---|
                 |--value stack(root)
                 |
                 |--request
                 |
                 |--parameters
                 |
                 |--attr (searches page, request, session, then application scopes)

refer this for more details

OGNL basis

basically struts2 put object value Stack as top most object and OGNL is used to refer them.root object can be referenced without any special "marker" or with % while References to other objects are marked with a pound sign (#). # is basically used to refer object/values from Application/Session etc.

烧了回忆取暖 2024-12-21 05:00:25

只是为了完成 @Devon Biere ...

使用 $(美元符号)

您也可以在资源文件中使用 ${}。 struts会自动使用OGNL来解析${}。例如,

sample.foo.bar=This is some ${someProperty} //If the someProperty is in valueStack
sample.welcome.message=Welcome dear ${#session.CurrentUser.farsiFirstName}

请注意这里的 $ 符号只是一个触发器,要求 struts 根据 OGNL 计算字符串,请不要将其与 JSTL

${} 混淆="https://stackoverflow.com/questions/32411365/struts-2-dynamic-message-with-ognl">Struts 2 动态消息与 OGNL

Just to complete the @Devon Biere ...

Use of $ (dollar sign)

You can use ${} in your resource files too. The struts will automatically use OGNL to parse ${}. For example

sample.foo.bar=This is some ${someProperty} //If the someProperty is in valueStack
sample.welcome.message=Welcome dear ${#session.CurrentUser.farsiFirstName}

Please note the $ sign here is just a trigger which ask struts to evaluate the string against OGNL, please do not confuse it with ${} in JSTL

Struts 2 Dynamic message with OGNL

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