2

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173

2 Answers2

2

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 ${}.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I am using the getText in an interceptor. So what do you think is the best way to do that. – Alireza Fattahi Sep 05 '15 at 11:09
  • You can use it in the interceptor because you can get the action instance there and it implements a `TextProvider`. IMHO, you can use `getText` in the action and interceptor free if you understand how it works, but in message resources you can also use `MessageFormat` variables. Whatever is the best way is up to you, personally I prefer the second way. – Roman C Sep 05 '15 at 12:45
1

I found that the vale stack already has the session in it with #session so

${#session.['CurrentUser'].farsiFirstName}
${#session.CurrentUser.farsiFirstName}

works fine.

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173