Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Diffrerence between ActionContext & ValueStack in struts 2

OGNL stands for Object-Graph Navigation Language; it is an expression language for getting and setting
properties of Java objects. You use the same expression for both getting and setting the value of a
property.
The struts 2 framework uses a standard naming context to evaluate OGNL expressions. 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).
The Action instance is always pushed onto the value stack. Because the Action is on the stack, and the
stack is the OGNL root, references to Action properties can omit the # marker. But, to access other
objects in the ActionContext, we must use the # notation so OGNL knows not to look in the root object,
but for some other object in the ActionContext.
|
|--application
|
|--session
context map---|
|--value stack(root)
|
|--request
|
|--parameters
|
|--attr (searches page, request, session, then application scopes)
|
Referencing an Action property
<s:property value="name"/>
Other (non-root) objects in the ActionContext can be rendered use the # notation.
<s:property value="#session.mySessionKey"/> or
<s:property value="#request['myRequestKey']"/>
The ActionContext is also exposed to Action classes via a static method.
ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);

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>
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.
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
I found that the vale stack already has the session in it with #session so
${#session.['CurrentUser'].farsiFirstName}
${#session.CurrentUser.farsiFirstName}

works fine.

You might also like