struts1国际化的切换

 在18、struts硬编码国际化的基础上,实现struts国际化的切换
1、了解利用struts默认将locale放到session中的特性,完成采用编程的方式切换语言设置
 * 参见:ChangeLanguageAction.java
2、在index.jsp页面中增加

<a href="changelang.do?lang=zh">中文</a>
<a href="changelang.do?lang=en">英文<a/>

 

33、ChageLanguageAction.java

package com.bjsxt.struts;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ChangeLanguageAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        //获得参数
        String lang = request.getParameter("lang");
        //得到当前的默认Locale
        Locale currentLocale = Locale.getDefault(); 
        if ("zh".equals(lang)) {
            currentLocale = new Locale("zh", "CN");
        }else if("en".equals(lang)) {
            currentLocale = new Locale("en", "US");
        }
        
        //在Action中有setLocale方法来设置Locale
        this.setLocale(request, currentLocale);
        //也可以用使用如下方法
        //request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
        return mapping.findForward("index");
    }

}

 

 

4、struts-config.xml加入配置信息

<action path="/changelang"
    type="com.bjsxt.struts.ChangeLanguageAction"
  >
   <forward name="index" path="/index.jsp"/>
  </action> 

 http://www.cnblogs.com/jhlishero/archive/2009/08/27/1554928.html

你可能感兴趣的:(apache,编程,jsp,struts,servlet)