Struts2 access ActionContext from jsp using property tag
-
25-06-2021 - |
Question
I have been looking up this information for a while, but does not seem like there is much online.
To make it simple, how do we access the ActionContext
through the <s:property/>
tag?
Basically I want to get the com.opensymphony.xwork2.ActionContext.locale
(current locale)
I tried all these, but none seems to work
<s:property value="#com.opensymphony.xwork2.ActionContext.locale"/>
<s:property value="${#com.opensymphony.xwork2.ActionContext.locale}"/>
<s:property value="%{#com.opensymphony.xwork2.ActionContext.locale}"/>
and more combinations of these.
Thanks
Solution
Have you tried locale.toString()
?
Locale: <s:property value='locale.toString()'/>
Edit
As you want the value from ActionContext
put this in your action
:
public class FooAction extends ActionSupport {
...
private String locale; // TODO: Getters and setters
...
@Override
public String execute () {
...
locale = ActionContext.getContext().getLocale().toString();
...
}
}
And then in your jsp you can access to the locale
attribute with <s:property>
.
<s:property value="locale"/>
I hope it helps.
OTHER TIPS
The more direct method would be to say:
<s:property value="@com.opensymphony.xwork2.ActionContext@getContext().locale"/>
For this to work static method invocation will need to be enabled in struts.xml
<struts>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
...
</struts>
You can use it as:
<%=session.getAttribute("WW_TRANS_I18N_LOCALE") %>
This will get the locale directly from session