What's the difference between # , % and $ signs in Struts tags?
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>
Accessing a HashMap using Struts 2
I suppose I was using a different version of struts wherein using the %{} was required for the expression to be evaluated. I changed the jar files now.
This is what did the job for me:
<s:property value="#mymap.[#mykey2]"/>
My problem was coming because I was trying to use it in a href for a s:a tag. And without the %{} operator, the expression was not being evaluated.
So, i guess, i was right in the beginning itself. Rest of the time, it was just me being silly. :>
Update:
I wrote a blog post on the issue, in case anyone is interested.
http://mycodefixes.blogspot.com/2010/11/struts-2-creating-and-accessing-maps.html
Freemarker and Struts 2, sometimes it evaluates as a sequence+extended_hash
The myText
could be a variable from the freemarker context, but if you want to use action property
<p>The text is: ${action.myText}</p>
Note, that action
prefix is not required to access action properties. A property resolution method is applied when resolving freemarker variables:
Property Resoloution:
Your action properties are automatically resolved - just like in a
velocity view.for example
${name}
will result instack.findValue("name")
, which
generally results inaction.getName()
being executed.A search process is used to resolve the variable, searching the
following scopes in order, until a value is found :
- freemarker variables
- value stack
- request attributes
- session attributes
- servlet context attributes
And later you can read what objects are accessible from the context.
Objects in the Context:
The following variables exist in the FreeMarker views
req
- the currentHttpServletRequest
res
- the currentHttpServletResponse
stack
- the currentOgnlValueStack
ognl
- theOgnlTool
instance
This class contains useful methods to execute OGNL expressions against arbitary objects, and a method to generate a select list using
the<s:select>
pattern. (i.e. taking the name of thelist
property, alistKey
andlistValue
)struts
- an instance ofStrutsBeanWrapper
action
- the current Struts actionexception
- optional theException
instance, if the view is a JSP exception or Servlet exception view
The error might be caused by searches from the value stack and returning something that you didn't expect depending on the structure of the stack at the moment of execution.
Adding a prefix to the variable to point out the exact location of the property should fix the redundancy in the code when searching in the value stack.
how to use struts2 tag to traversal a MapObject,String in jsp
You do not need second iterator to get Course
data. Just use key
to get your values.
<s:iterator value="cmap">
<tr>
<td><s:property value="key.coursename"/></td>
<td><s:property value="value"/></td>
</tr>
</s:iterator>
BTW a map with a key which is custom object is very annoying thing.
How to choose a value of a HashMap in select Struts2?
If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.
But you need to change this rule
<s:select label="Pick the company name"
headerKey="-1" headerValue="Select Company name"
list="%{companyMap.entrySet()}"
name = "companyId"
listKey="value"
listValue="key"
/>
struts2 iterator tag help
for someone else trying to do this...
Best way is to create the list in your class like so:
public void generateIteratorList()
{
iterList = new ArrayList();
int value = 0;
for (int i = 0; i < (getSecondResultSet().size()/3); i++)
{
iterList.add(value);
value+=3;
}
}
and then use s:iterator tag like so:
<s:iterator status="stat" value="iterList" >
<s:property value="%{secondResultSet.get(top).altId}" />
<s:property value="%{secondResultSet.get(top+1).altId}" />
<s:property value="%{secondResultSet.get(top+2).altId}" />
</s:iterator>
What are the differences between ${} and #{}?
#{}
are for deferred expressions (they are resolved
depending on the life cycle of the page) and can be used to read or
write from or to a bean or to make a method call.${}
are expressions for immediate resolution, as soon as they are
encountered they are resolved. They are read-only.
You can read more here: http://docs.oracle.com/javaee/6/tutorial/doc/bnahr.html
Related Topics
Finding Number of Cores in Java
Priorityqueue Not Sorting on Add
Java - How to Receive Point Coordinates After Mouse Button Release (Jfreechart)
Should You Always Code to Interfaces in Java
Comparing Time Is Incorrect When Picking 12:00
Difference Between Double and Double in Comparison
How to Sort Two Arrays in Relation to Each Other
How to Compile a Java Source File Which Is Encoded as "Utf-8"
Why Is Executing Java Code in Comments with Certain Unicode Characters Allowed
Where Is Java Installed on MAC Os X
Can't a Swing Component Be Added to Multiple Containers
Differencebetween 'E', 'T', and '' for Java Generics