javastruts2actioncontext

Struts2 access ActionContext from jsp using property tag


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.