2013-08-20 28 views
3

我有一个控制器,它设置一些Flash属性并调用重定向。我还有一个前置控制器拦截器,可以拦截重定向的URL并强制重定向。此时我的Flash属性被删除,因为spring认为重定向目标已经完成。重定向完成后保持Spring的flash属性

我想维护这些属性,以便我的第二个控制器可以在第二次重定向后访问它们。

任何可能的实现方式?

请注意,我不能更改第一个控制器,它最初将它们填充,我需要这些属性才能到达第二个重定向控制器。

感谢

+0

将它们添加到新注入的'RedirectAttributes'对象中,或者将它们放入'HttpSession'中。 –

+0

让我澄清: – bahaa

+0

让我澄清一下:控制器A填充RedirectAttributes对象并重定向到控制器B.控制器B永远不会到达,因为拦截器会强制重定向到控制器C.在我的拦截器中,我可以访问请求,响应,和handlermethod。我只能将代码添加到控制器C或拦截器。你建议我回顾他们? – bahaa

回答

4

HandlerInterceptor在里面,你应该做到以下几点

FlashMap lastAttributes = RequestContextUtils.getInputFlashMap(request); // should hold the attributes from your last request  
FlashMap forNextRequest = RequestContextUtils.getOutputFlashMap(request); // will hold the attributes for your next request 
forNextRequest.putAll(lastAttributes); 
0

我知道已经有一个公认的答案,但我想添加一些细节@Sotirios Delimanolis的答案。

我要做的就是:

Map<String, ?> previousFlashAttributes = RequestContextUtils.getInputFlashMap(request); 
FlashMap flashAttributesForNextRequest = RequestContextUtils.getOutputFlashMap(request); 
if (previousFlashAttributes != null && flashAttributesForNextRequest != null) { 
    flashAttributesForNextRequest.putAll(previousFlashAttributes); 
    RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashAttributesForNextRequest, request, response); 
} 

最后一行保存FlashMap到输出。