Struts2 tags seem to evaluate inconsistently

后端 未结 2 1087
你的背包
你的背包 2021-01-22 22:09

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

相关标签:
2条回答
  • 2021-01-22 22:56

    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.

    0 讨论(0)
  • 2021-01-22 23:11

    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"/>
    
    0 讨论(0)
提交回复
热议问题