1 16 17 package org.springframework.web.servlet.i18n; 18 19 import java.util.Locale ; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.springframework.beans.propertyeditors.LocaleEditor; 26 import org.springframework.web.servlet.LocaleResolver; 27 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 28 import org.springframework.web.servlet.support.RequestContextUtils; 29 30 38 public class LocaleChangeInterceptor extends HandlerInterceptorAdapter { 39 40 43 public static final String DEFAULT_PARAM_NAME = "locale"; 44 45 private String paramName = DEFAULT_PARAM_NAME; 46 47 48 52 public void setParamName(String paramName) { 53 this.paramName = paramName; 54 } 55 56 57 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 58 throws ServletException { 59 60 String newLocale = request.getParameter(this.paramName); 61 if (newLocale != null) { 62 LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); 63 if (localeResolver == null) { 64 throw new IllegalStateException ("No LocaleResolver found: not in a DispatcherServlet request?"); 65 } 66 LocaleEditor localeEditor = new LocaleEditor(); 67 localeEditor.setAsText(newLocale); 68 localeResolver.setLocale(request, response, (Locale ) localeEditor.getValue()); 69 } 70 return true; 72 } 73 74 } 75 | Popular Tags |