Search code examples
jspweb-applicationsstruts2ognl

Nested OGNL expression is not rendered


I'm having a problem, and i don't really know why is this happening, i'm pretty new with OGNL... So i have a layout which has defined a tile, another tile extends this layout and puts a key which is used to show the value on the DB of that key, let's say is called keyName, i wanna render that value using OGNL like this:

<s:label value="%{getText('%{keyName}')}"/>

but is showing the putted value of keyName not the value of the DB..., if i tried hardcoding the keyValue and works ok, for example:

<s:label value="%{getText('strWelcome')}"/>

This show correctly the value of the key strWelcome in the DB... Any advice of how to fix it??

NOTE: i tried using the keyName like a JSTL variable but tells me that i can't pass dynamic values to getText().


Solution

  • OGNL does not work that way, it either is or is not an OGNL expression, you don't say "this is an OGNL expression" and then inside the expression say "oh this is an OGNL expression", you only need to do it once.

    The end result is these are probably the correct expressions:

    <s:label value="%{getText(keyName)}"/> 
    

    being a value attribute I suspect OGNL is assumed so this should work

    <s:label value="getText(keyName)"/> 
    

    This assumes that the variable "keyName" is in the actions scope, should it be in a different scope you might need to append a "#" or you could be more specific, for details on accessing different scopes see: http://struts.apache.org/release/2.2.x/docs/ognl.html