2014-10-29 83 views
0
@Autowired 
private SessionLocaleResolver localeResolver; 

@Override 
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
     Authentication authentication) throws IOException, ServletException { 
    // clean previous failed access 
    authorizationServiceConfig.cleanUserFailedAccess(); 

    // set the session locale 
    localeResolver.setLocale(request, response, authorizationServiceConfig.getUserLocale()); 

    //redirect login.. 
    handle(request, response, authentication); 

} 

我需要获取我之前在身份验证服务中设置的SessionLocaleResolver语言环境值。但我没有在这里请求:(如何获取当前会话区域设置?

public String getMessage(String key) { 
    String answer; 

    if (key == null) { 
     return ""; 
    } 
    try { 
     answer = getMessageSourceAccessor().getMessage(key, "here i need the Session locale"); 
    } catch (Exception e) { 
     answer = key; 
    } 
    return answer; 
} 

回答

0

如果你想做的,如果要获得一个没有指向它的指针的方法的当前请求,RequestContextHolder是你的朋友。由于这是一个共同的要求,春店在一个线程局部变量的对象,并给出了通过这个类的静态方法获得它:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder 
           .getRequestAttributes()).getRequest(); 

当然,如果这样工作在servlet应用程序,而不是在门户的 - 你将不得不使用PortletRequestAttributes和finaly得到一个PortletRequest

+0

你怎么能假设Spring WebFlow的用法? – dimitrisli 2014-10-29 19:52:58

+0

'RequestContextHolder'由'DispatcherServlet'自动启用。如果不使用DispatcherServlet,那么在ServletContext中注册一个RequestContextListener就足够了。它没有依赖Spring WebFlow – 2014-10-29 21:40:35

+0

aaah你是对的。我知道org.springframework.webflow.execution.RequestContextHolder ..显然有一个org.springframework.web.context.request.RequestContextHolder – dimitrisli 2014-10-29 21:45:09