Struts 2 Dynamic message with OGNL

我的梦境 提交于 2019-12-22 14:12:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!