Question

I googled 2 hours for my issue but still could'nt find any sollution.

What I want to do:

I'm using struts2 for an international application so I need multi-language support. Therefor I found the i18n-Interceptor to change the locale with only one parameter to pass through (very cool!). Now I want to change the locale at any point in my application, so that the user can change the language and would be forwarded to the same page or action he cames from. This seems not so easy. I've already tried the following things found on google, but doesn't work for me:

  1. Use the acutall URL/URI on the JSP (like "ShowUser.action") and call this with another locale -> No way found to get the full URL or URI in jsp with parameters. URL gives me the current site from tiles (i.e. /content/site/user.jsp) and URI returns me my tiles-layout (i.e. /content/layout.jsp). I've tried request.getRequestUrl() and request.getRequestURI()

  2. Use the actionContext to get the full URL for JSP -> no way found..

  3. Use the "request.getHeader("referal") in my "ChangeLocale.action" and set this dynamically as redirectAction in struts.xml -> Working but too much code to parse the URL with parameters, etc.)

The third point is working with the following code, but I'm looking for a nicer sollution:

The relevant part in struts.xml

  <action name="Locale" class="de.smg.LocaleAction">
     <interceptor-ref name="i18n"/>
     <interceptor-ref name="basicStack"/>
     <result type="redirectAction">${forwardAction}</result>
  </action>

The LocaleAction.java:

public class LocaleAction extends ActionSupport implements ServletRequestAware {

private static final long serialVersionUID = 1L;
private static final String FALL_BACK_ACTION = "ShowStartPage";

private HttpServletRequest request;
private String forwardAction = null;

@Override
public String execute() throws Exception {
    String referer = request.getHeader("referer");
    String action = FALL_BACK_ACTION;

    if (referer!=null) {        
        // WeberT: first cut off the protocol, server and port...
        referer = referer.substring(referer.lastIndexOf("/") + 1, referer.length());

        // WeberT: then cut of the .action part if available
        int dotPos = referer.indexOf(".");

        if (dotPos>0) {
            int qMarkPos = referer.indexOf("?");
            if (qMarkPos> 0 && dotPos < qMarkPos) {         
                action = referer.substring(0, dotPos) + referer.substring(qMarkPos, referer.length());
            } else {
                action = referer.substring(0, dotPos);                  
            }               
        }
        forwardAction = action;
    }
    return SUCCESS;
}

@Override
public void setServletRequest(HttpServletRequest request) {
    this.request = request;     
}

/**
 * @return the forwardAction
 */
public String getForwardAction() {
    return forwardAction;
}
 }  

So I came i.e. from the action:

http://server:port/ShowUser.action?id=1

And on the user Site I switch to another language my LocaleAction will return the forwardAction:

ShowUser?id=1

So, this is working but seems to me like the dark side? What is the right solution to solve this (as I thought) "little" problem? Thanks for your answers.

Best regards Thomas :-)

Was it helpful?

Solution

Use <s:url> tag with includeParams attribute set to all and with empty value attribute.

<s:url var="urlen" includeParams="all" value="">
  <s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{urlen}">English</s:a>

You don't need any special action configuration for that, but i18n interceptor must be in the interceptors stack (it is already included in the defaultStack).

OTHER TIPS

I am not sure what exactly you want to get out from the ActionContext, but most of the information you can find out from ActionContext and here are some of it

Object action=ActionContext.getContext().getActionInvocation().getAction();
ActionProxy actionProxy=ActionContext.getContext().getActionInvocation().getProxy();
String methodName=actionProxy.getMethod();
String context = actionProxy.getName();
String namespace= actionProxy.getNamespace();
ActionContext.getContext().getParameters() //parameters from the UI

so if you see you have almost all information you need to get your work done.The ActionContext is the context in which an Action is executed. Each context is basically a container of objects an action needs for execution like the session, parameters, locale, etc.

Best thing about ActionContext is thread local which means that values stored in the ActionContext are unique per thread.

Alternatiively you can get the current URL with help of javascript also.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top