I have an issue with some Struts2 tags mixed with OGNL. To overcome the fact that you cannot nest evaluation of expressions in OGNL, e.g., %{foo[%{bar}]}
, I use suc
First, you are not completely correct saying that you cannot nest evaluation of expressions in OGNL. If you look at the Expression Evaluation of the OGNL Language Gude, you will find that if you use parenthesized expression without a dot in front of the parentheses the result of evaluating that expression will be used as expression to be evaluated. See the example here.
Second, I don't see the reason for which you are getting indexes other than iterator index.
Third, the set
tag places variables to the value stack context that is accessible via #
notation. See the examples here.
Fourth, answering your direct question:
Because a typo in the fourth line. Replace the line with
<s:set var="grpName" value="model[%{#grpIndex}].groupName"/>
will do what expected. You can see the differences in expression.
The last is just reference to show you where you should use %{}
to force evaluation of expression, because Struts is parsing everything (by default) in the tag attributes for OGNL.
The usage of %{} notation:
Used in OGNL to force evaluate the content in brackets as OGNL expression.
First of all you are using <s:text>
(line 3) instead of <s:property>
which will yield a wrong test results. Second there is a typo in line 4 (unopened bracket). And most important don't use %{}
inside []
in <s:property>
or <s:set>
tags.
<s:set var="grpIndex" value="options[#optstatus.index]"/>
grpIndex = <s:property value="#grpIndex"/><br/>
grpName = <s:property value="model[#grpIndex].groupName"/><br/>
<s:set var="grpName" value="model[#grpIndex].groupName"/>
groupName = <s:property value="#grpName"/>