问题
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.
回答1:
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 ${}
.
回答2:
I found that the vale stack already has the session in it with #session
so
${#session.['CurrentUser'].farsiFirstName}
${#session.CurrentUser.farsiFirstName}
works fine.
来源:https://stackoverflow.com/questions/32411365/struts-2-dynamic-message-with-ognl