1 18 package org.apache.struts.webapp.example; 19 20 import java.util.Locale ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.HttpSession ; 25 26 import org.apache.struts.Globals; 27 import org.apache.struts.action.ActionForm; 28 import org.apache.struts.action.ActionForward; 29 import org.apache.struts.action.ActionMapping; 30 31 32 35 public final class LocaleAction extends BaseAction { 36 37 42 private boolean isBlank(String string) { 43 return ((string==null) || (string.trim().length()==0)); 44 } 45 46 50 private static final String LANGUAGE = "language" ; 51 52 56 private static final String COUNTRY = "country"; 57 58 61 private static final String PAGE = "page"; 62 63 67 private static final String FORWARD = "forward"; 68 69 72 private static final String LOCALE_LOG = "LocaleAction: Missing page or forward parameter"; 73 74 92 public ActionForward execute(ActionMapping mapping, 93 ActionForm form, 94 HttpServletRequest request, 95 HttpServletResponse response) 96 throws Exception { 97 98 String language = request.getParameter(LANGUAGE); 99 String country = request.getParameter(COUNTRY); 100 101 Locale locale = getLocale(request); 102 103 if ((!isBlank(language)) && (!isBlank(country))) { 104 locale = new Locale (language, country); 105 } 106 else if (!isBlank(language)) { 107 locale = new Locale (language, ""); 108 } 109 110 HttpSession session = request.getSession(); 111 session.setAttribute(Globals.LOCALE_KEY, locale); 112 113 String target = request.getParameter(PAGE); 114 if (!isBlank(target)) return new ActionForward(target); 115 116 target = request.getParameter(FORWARD); 117 if (isBlank(target)) target = mapping.getParameter(); 118 if (isBlank(target)) { 119 log.warn(LOCALE_LOG); 120 return null; 121 } 122 return mapping.findForward(target); 123 } 124 } 125 | Popular Tags |