简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM