1struts.xml配置
<!-- 国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="doraemon" />
2添加配置文件
doraemon_en_US.properties和doraemon_zh_CN.properties
3,在页面中添加中英文切换的链接
<a href="/Doraemon/language.action?request_locale=en_US">English </a>|<a href="/Doraemon/language.action?request_locale=zh_CN"> 简体中文</a>
4struts配置文件中添加对应的跳转配置
<action name="language" class="com.csljc.doraemon.common.action.SwitchLanguageAction" method="chooseLanguage">
<result name="index" >/CommonFiles/Home.jsp</result>
<result name="success" >/login.jsp</result>
</action>
5com.csljc.doraemon.common.action.SwitchLanguageAction的代码如下:
package com.csljc.doraemon.common.action;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class SwitchLanguageAction extends ActionSupport {
private String referer;
private static Logger logger = Logger.getLogger(SwitchLanguageAction.class);
// @Action(value = "/chooseLanguage", results = { @Result(name = SUCCESS, type="redirect", location = "${referer}") })
public String chooseLanguage() throws Exception {
HttpServletRequest request= ServletActionContext.getRequest();
HttpServletResponse response= ServletActionContext.getResponse();
String request_locale = request.getParameter("request_locale");
if (request_locale.equals("zh_CN"))
request.getSession().setAttribute("WW_TRANS_I18N_LOCALE",
java.util.Locale.CHINA);
if (request_locale.equals("en_US"))
request.getSession().setAttribute("WW_TRANS_I18N_LOCALE",
java.util.Locale.US);
Cookie cookie = new Cookie("CYOU_LANG",
request_locale);
cookie.setMaxAge(Integer.MAX_VALUE);
cookie.setPath("/");
response.addCookie(cookie);
logger.debug(request_locale);
referer = request.getHeader("referer");
if ( (referer==null || "".equals(referer.trim())) ) {
referer = "/test/toIndexPage.action";
}
return SUCCESS;
}
}
6在前台页面添加
<s:text name="username"/>
选择语言的链接只有在改上下文中存用local信息,只用通过该action跳转的页面才能国家化,为了让所有页面国际化要在每个action加过滤器以便是每个action上下文中都存有local的编码信息,
7过滤器代码如下:
package com.csljc.doraemon.common.filter;
import java.io.IOException;
import java.util.Locale;
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.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
public class LanguageFilter implements Filter
{
private static Logger logger = Logger.getLogger(LanguageFilter.class);
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String request_locale = request.getParameter("request_locale");
logger.debug("---------request_locale:"+request_locale);
Locale locale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE");
if(locale == null)
{
Cookie[] allcookies = request.getCookies();
if(allcookies!=null)
{
for(int i=0;i<allcookies.length;i++)
{
if(allcookies[i].getName().equalsIgnoreCase("CYOU_LANG"))
{
System.out.println("-----卡卡西————————"+allcookies[i].getValue());
if(allcookies[i].getValue().equalsIgnoreCase("zh_CN")){
locale = Locale.US;
System.out.println("--中文--------language:"+locale);
}
else if(allcookies[i].getValue().equalsIgnoreCase("zh_TW")){
locale = Locale.TAIWAN;
}
else if(allcookies[i].getValue().equalsIgnoreCase("en_US")){
locale = Locale.US;
System.out.println("--英文--------language:"+locale);
}
}
}
}
}
if(locale==null)
{
locale = request.getLocale();
}
logger.debug(request.getLocale());
session.setAttribute("WW_TRANS_I18N_LOCALE", locale);
logger.debug("-----------language:"+locale);
System.out.println("-----------language:"+locale);
arg2.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
8在web.xml中注册过滤器。
<!-- 国际化 过滤器 -->
<filter>
<filter-name>LanguageFilter</filter-name>
<filter-class>com.csljc.doraemon.common.filter.LanguageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LanguageFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
PS:在登陆页面有<s:text name="username"/>时,报异常:
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location] org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
错误原因是:在web.xml中struts过滤器的url匹配不是“/*”,不为/*时有的页面不会调用sturts的默认过滤器,国际化过滤器不被调用因此有
<s:text>标签时报异常。要把url匹配改为“/*”