What's the difference between # , % and $ signs in Struts tags?

Khashayar picture Khashayar · Nov 4, 2011 · Viewed 43.9k times · Source

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?

Answer

Devon Biere picture Devon Biere · Jul 23, 2012

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>