Professional Documents
Culture Documents
Diffrerence Between ActionContext
Diffrerence Between ActionContext
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);
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" />)
#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
|
|--application
|
|--session
context map---|
|--value stack(root)
|
|--request
|
|--parameters
|
|--attr (searches page, request, session, then application scopes)
works fine.