2014-10-29 74 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

相關問題