Pardon me if this question has been asked before, but I haven\'t gotten a straight answer that helped me solve my problem. I have a gwt application that I have secured using sp
Thanks @Ritesh for give the answer. You give me a idea to handle this problem. I worked around it on a further way:
public class InternationalizationFilter extends OncePerRequestFilter {
private Logger log=Logger.getLogger(InternationalizationFilter.class);
private String localeParam="lang";
private LocaleResolver localeResolver;
public InternationalizationFilter(String localeParam, LocaleResolver localeResolver) {
this.localeParam = localeParam;
this.localeResolver = localeResolver;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
protected void doFilterInternal(
final HttpServletRequest request,
final HttpServletResponse response,
final FilterChain filterChain)
throws ServletException, IOException {
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
else{
final String newLocale = request.getParameter(localeParam);
if (newLocale != null) {
final Locale locale = StringUtils.parseLocaleString(newLocale.toLowerCase());
LocaleContextHolder.setLocale(locale);
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(newLocale);
localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
log.debug("change locale to "+
locale.getLanguage()+"_"+locale.getCountry()+
" at Thread"+Thread.currentThread().getId());
}
else{
final Locale locale=localeResolver.resolveLocale(request);
LocaleContextHolder.setLocale(locale);
log.debug("restore locale to "+
locale.getLanguage()+"_"+locale.getCountry()+
" at Thread"+Thread.currentThread().getId());
}
try {
filterChain.doFilter(request, response);
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
}
}
With declared the localChangeInterceptor and localeResolver in mvc config file. So we can change locale with request param "lang":
In the security config file,I made a i18nFilter and added it to the Filter Chain:
with localChangeInterceptor & localeResolver,I can change locale and save locale into session or cookie.
under localeResolver's help,InternationalizationFilter could store/restore locale and make change to LocaleContextHolder's locale.