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:
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()
Use the actionContext to get the full URL for JSP -> no way found..
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 :-)
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
).