2012-01-03 85 views
0
public class FlashMapFilter extends OncePerRequestFilter { 

    @Override 
    @SuppressWarnings("unchecked") 
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 
      throws ServletException, IOException { 
     HttpSession session = request.getSession(false); 
     if (session != null) { 
      Map<String, ?> flash = (Map<String, ?>) session.getAttribute(FlashMap.FLASH_MAP_ATTRIBUTE); 
      if (flash != null) { 
       for (Map.Entry<String, ?> entry : flash.entrySet()) { 
        Object currentValue = request.getAttribute(entry.getKey()); 
        if (currentValue == null) { 
         request.setAttribute(entry.getKey(), entry.getValue()); 
        } 
       } 
       session.removeAttribute(FlashMap.FLASH_MAP_ATTRIBUTE); 
      } 
     } 
     filterChain.doFilter(request, response); 
    } 
} 

而不是每个请求显示一次Flash消息,这会显示一条消息,消息之前的两个完整请求的持续时间。这是为什么?为什么我的FlashMapFilter显示两个请求,即使它扩展了OncePerRequestFilter?

回答

0

每个请求一次意味着doFilterInternal()将不会在单个请求中被调用两次。要获得更好的闪光范围支持,请参见Spring 3.1 Using flash attributes

至于你的代码你试过把断点放在session.removeAttribute

1

首先确保您只在会话中设置属性。

此外,请确保您不会呈现在会话中设置的唯一一个来自请求的设置。你如何渲染它?使用JSP EL?那么你必须明确地使用${requestScope.flashScopeAttribute},因为${flashScopeAttribute}也会搜索sessionScope。

相关问题