Search code examples
jspstruts2elresourcebundleognl

Struts 2 Dynamic message with OGNL


In a Struts 2 project, consider below key in message resources:

export.filename.accountsummary=customer ${export}

The action has export filed with setter and getter. If you call the getText("export.filename.accountsummary") struts will automatically call the getExport() and fill it with correct value. (I think that struts use OGNL too lookup the resource messages that are endorse with ${}) Is this correct ?!

I try to use customer ${#sessionScope.CurrentUser.userName} an expected that struts use this OGNL expression but it did not worked.


Solution

  • Looks like the variable sessionScope is not available in the context (if you didn't put it manually). Don't mess it up with JSP session scope variable (the syntax is similar that is used in JSP for EL, but Struts2 doesn't use JSP EL engine there), everything in OGNL expression evaluated against the OGNL context. You can use ${} syntax in messages, Struts parses its value for OGNL expression, and this syntax defines a scope of the expression, which is evaluated after removing ${}.