我在谷歌上搜索了两个小时,但仍然找不到任何解决方案。
我想做的事:
我正在为一个国际应用程序使用struts2,所以我需要多语言支持。因此,我找到了i18n Interceptor,只需通过一个参数即可更改区域设置(非常酷!)。现在,我想在应用程序的任何时候更改区域设置,这样用户就可以更改语言,并被转发到他访问的同一页面或操作。这似乎不那么容易。我已经尝试了在谷歌上找到的以下东西,但对我不起作用:
-
使用JSP上的acutallURL/URI(如"ShowUser.action"),并用另一个区域设置调用它->在JSP中找不到带参数的完整URL或URI。URL从tiles(即/content/site/user.jsp)中给我当前的站点,URI返回我的tiles布局(即/contant/layout.jsp)。我已经尝试了request.getRequestUrl()和request.getRequest URI()
-
使用actionContext获取JSP->no-wayfound的完整URL。。
-
使用我的"ChangeLocale.action"中的"request.getHeader("referal"),并在struts.xml->Working but too much code to parse the URL with parameters,etc."中将其动态设置为redirectAction。)
第三点是使用以下代码,但我正在寻找更好的解决方案:
struts.xml 中的相关部分
<action name="Locale" class="de.smg.LocaleAction">
<interceptor-ref name="i18n"/>
<interceptor-ref name="basicStack"/>
<result type="redirectAction">${forwardAction}</result>
</action>
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;
}
}
所以我来了,即来自行动:
http://server:port/ShowUser.action?id=1
在用户网站上,我切换到另一种语言,我的LocaleAction将返回forwardAction:
ShowUser?id=1
所以,这是有效的,但在我看来似乎是黑暗的一面?解决这个(正如我所想的)"小"问题的正确解决方案是什么?谢谢你的回答。
向托马斯致以最良好的问候:-)
使用<s:url>
标记,includeParams
属性设置为all
,value
属性为空。
<s:url var="urlen" includeParams="all" value="">
<s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{urlen}">English</s:a>
您不需要任何特殊的操作配置,但i18n
拦截器必须在拦截器堆栈中(它已经包含在defaultStack
中)。
我不确定你到底想从ActionContext
中得到什么,但你可以从ActionContext
中找到大多数信息,这里是
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
因此,如果你看到自己几乎掌握了完成工作所需的所有信息。ActionContext
是执行Action的上下文。每个上下文基本上都是操作执行所需对象的容器,如会话、参数、区域设置等。
ActionContext
最好的地方是线程本地,这意味着存储在ActionContext
中的值对于每个线程是唯一的。
或者,您也可以在javascript的帮助下获得当前URL。