如何使Filter在同一请求上获取LocaleChangeInterceptor的结果

时间:2015-10-12 02:32:30

标签: spring spring-mvc servlets servlet-filters

我有一个过滤器,我在其中设置要在JSP上显示的属性。

@Component
public class EnvironmentFilter extends OncePerRequestFilter
{
    @Autowired
    private LocaleResolver localeResolver;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException,
    IOException
    {
      Locale currentLocale = localeResolver.resolveLocale( request );
      request.removeAttribute( KEY_BUYSIDE_NAME );
      if (Locale.ENGLISH.equals( currentLocale ))
      {
          request.setAttribute( "companyName", company.getName() );
      }
      else
      {
          request.setAttribute( "companyName", company.getLocalName() );
      }
    }
}

但似乎在同一请求中,过滤器在LocaleChangeInterceptor之前调用。因为我得到的语言总是很晚。例如,当我用日语将语言设置为英语时,我会得到日语。当我稍后点击日语时,我会得到英语。我的情况总是迟到。

1 个答案:

答案 0 :(得分:0)

对于遇到同样问题的人,我找到了解决方法。我实施了:

var dataAccess = Assembly.GetExecutingAssembly();

builder.RegisterAssemblyTypes(dataAccess)
   .Where(t => t.Name.EndsWith("Query"))
   .AsImplementedInterfaces();

我从LocaleChangeInterceptor

复制了代码