Struts 2 Dynamic message with OGNL
Asked Answered
D

2

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.

Doerrer answered 5/9, 2015 at 9:12 Comment(0)
W
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 ${}.

Witherite answered 5/9, 2015 at 9:43 Comment(2)
I am using the getText in an interceptor. So what do you think is the best way to do that.Doerrer
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.Witherite
D
1

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

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

works fine.

Doerrer answered 6/9, 2015 at 14:3 Comment(1)
Why do you marked answer that doesn't answer your question as accepted? Upvote is enough in that case.Shape

© 2022 - 2024 — McMap. All rights reserved.