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

3

79

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?

Eaglestone answered 4/11, 2011 at 10:26 Comment(0)
A
116

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>

About answered 23/7, 2012 at 21:31 Comment(3)
About $ please note that the <s:property> is xss hack safe while ${} is not. Use ${} with care!Huntsville
Exactly, as we discussed in your Q&A, that I'm linking because it could be useful to future readersZincography
Do not count on allowStaticMethodAccess it will be removed from struts framework soon #28019361Huntsville
C
10

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.

Credo answered 4/11, 2011 at 10:37 Comment(10)
<s:iterator value="places" status="portStatus2"> <tr> <td> <s:a href="#" onclick="javascript:window.open('%{link}');"><s:property value="name"/></s:a> </td> </tr> </s:iterator>Eaglestone
what link is here as i can't see this, but accordingly it should be the top most element in value stack or if you are using iterator and refereeing its value that means when iterator is moving through the value e=it placing each individual element on top of value stack one at a time and %{link} is refereeing that top stack valueCredo
Sorry I'm New in this Site I meant in my program i have list called places and I'm doing this! <s:iterator value="l" status="portStatus"> <s:if test="%{#portStatus.count == page+1}"> <tr> <td rowspan="11" width="50%" align="right"> <img src="${imagePath}" alt="Logo" /> </td> </tr> <tr> <td> <s:a href="#" onclick="javascript:window.open('%{link}');"><s:property value="name"/></s:a> </td> </tr> </s:iterator> This is working but i dont know why i'm putting this stuff there!! i just try them all to get answer!!Eaglestone
first better for you to learn about struts2 basic how request processing goes in to it what value stack is and how OGNL is working inside it, that will help you to understand better.Credo
Thats good since in-order to understand you need to first understand how Struts2 framework is pushing the object in value stack and how we can access thatCredo
@umeshawasthi It's not so much that it "appears to be a single object", but that OGNL will traverse down the stack until it finds something that can satisfy an expression. That's actually important, because the order of which things are pushed onto the stack, and what's pushed when, can be the cause (or the solution!) to a few types of problems.Rothstein
@dave:i am agree with you and i am aware about how ognl traverse in value stack but yes put wrong words in explaining.Thanks for this !!Credo
@umeshawasthi It was for for Khashayar but I can only tag with one person :)Rothstein
@dave :) but still i explained about top most object on value stack and not how OGNL resolve property value down the stackCredo
@DaveNewton thanks Dave, at first i was just wanted to know what they do but now that i read the whole thing i exactly get whats going on! It's Always better to do something completely from the beginning !Eaglestone
H
0

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

Huntsville answered 8/9, 2015 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.