日期:2014-05-18 浏览次数:20885 次
import org.apache.struts.Globals; import java.util.Locale; import javax.servlet.http.Cookie; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm frm = (LoginForm) actionForm; String param="0"; Cookie[] allcookie=servletRequest.getCookies(); if(frm.getLanguage()==null){ //首先获取cookie for(int i=0;i<allcookie.length;i++){ if(allcookie[i].getName().equals("language")){ param=allcookie[i].getValue(); break; } else{ //set cookie default 0 Cookie cookie=new Cookie("language",param); cookie.setMaxAge(60*60*24*365); //一年 servletResponse.addCookie(cookie); } } } else{ //set cookie Cookie cookie=new Cookie("language",frm.getLanguage()); //设置 cookie 0 cookie.setMaxAge(60*60*24*365); //30*60 半小时 servletResponse.addCookie(cookie); param=frm.getLanguage(); } switch(new Integer(param).intValue()){ case 0: servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA); break; case 1: servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH); break; default: servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH); break; } return actionMapping.findForward("index"); }