参考文章:STRUTS 2 国际化并自定义切换语言的问题解决
一,国际化resource文件配置及使用
参考百度
二,国际化切换
1,切换用Link
调用anction时,将当前界面URL作为参数传递,确保语言切换后可以返回当前界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<div>
<a href="<%=pageContext.getAttribute("basePath")%>lang.action?local=zh_CN¤tUrl=<%=request.getServletPath()%>">中文</a>
<a href="<%=pageContext.getAttribute("basePath")%>lang.action?local=en_US¤tUrl=<%=request.getServletPath()%>">English</a>
</div>
2,切换用Action
使用参数,更改Session中的WW_TRANS_I18N_LOCALE信息
package gravehistory.test.struts;
import java.util.Locale;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LangAction extends ActionSupport {
private String local = "";
private String currentUrl = "";
public String execute() {
if (local != null) {
String loc[] = local.split("_");
Locale locale = new Locale(loc[0], loc[1]);
// Session 值设定。第一句代码很重要,不设定的话,需要点两次切换按钮才可以切换
// 如果URL request参数名为request_locale,可以不设定
ActionContext.getContext().setLocale(locale);
ServletActionContext.getRequest().getSession()
.setAttribute("WW_TRANS_I18N_LOCALE", locale);
}
return "success";
}
public String getCurrentUrl() {
return currentUrl;
}
public void setCurrentUrl(String currentUrl) {
this.currentUrl = currentUrl;
}
public String getLocal() {
return local;
}
public void setLocal(String local) {
this.local = local;
}
}
Action执行结束后,返回调用页面
<action name="lang" class="gravehistory.test.struts.LangAction"> <result name="success">${currentUrl}</result> </action>
3,切换用Filter
如果想要将当前A.jsp界面选择的的语言环境同时应用于其他界面(B.jsp,C.jsp。。。)时,需要做一下处理。
GHRequestWrapper和I18nFilter的作用是,根据Session中WW_TRANS_I18N_LOCALE重新创建Local,以代替Struts从浏览器获取的Local。
package com.daipeng.gravehistory.filter;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
public class GHRequestWrapper extends HttpServletRequestWrapper {
private Locale locale = null;
public GHRequestWrapper(HttpServletRequest request) {
super(request);
HttpSession session = request.getSession();
locale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE");
}
@Override
public Locale getLocale() {
if (locale != null) {
return locale;
}
return super.getLocale();
}
}
package com.daipeng.gravehistory.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class I18nFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest r = (HttpServletRequest) req;
GHRequestWrapper request = new GHRequestWrapper(r);
filterChain.doFilter(request, resp);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
XML中如果不配置<dispatcher>FORWARD</dispatcher>等的话,某些情况下(如URL重定向等)过滤器失效
<filter-mapping> <filter-name>i18nFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>