如何在调用 DispatcherServlet 之前访问过滤器链中的 spring-mvc flash redirect



我有以下控制器:

@Controller
@RequestMapping("/my-account")
public class AccountController {
    @RequestMapping(value = "/foo/post",
            method = RequestMethod.POST)
    public String doPost(final RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("flashAttribute", "flashAttributeValue");
        return "redirect:/my-account/foo/get";
    }
    @RequestMapping(value = "/foo/get",
            method = RequestMethod.GET)
    public void doGet(final HttpServletRequest request, final Model model) {
        System.out.println("in request: " + RequestContextUtils.getInputFlashMap(request).get("flashAttribute"));
        System.out.println("in model: " + model.asMap().get("flashAttribute"));
    }
}

我还想在调用过滤器链中的过滤器期间访问 flash 属性flashAttribute,该过滤器最终调用弹簧默认DispatcherServlet,而弹簧默认进而调用AccountController .

public class FlashAttributeBasedFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
            throws ServletException, IOException {
        String flashAttribute = // how to access the redirectAttribute flashAttribute here?
        // do something with flashAttribute ...
        filterChain.doFilter(request, response);
}

DispatcherServlet使用处理这些 flash 属性的org.springframework.web.servlet.FlashMapManager,但它不提供只读访问权限,所以我想如果我在过滤器中使用它,我会搞砸一些东西。而且FlashMapManager实例也私下保存在调度程序 servlet 中。

有没有人知道我如何使重定向属性在POST之后的GET请求的过滤器链中可访问?

考虑到所有这些方法都会null返回到我的过滤器中(我不明白为什么(:

RequestContextUtils.getFlashMapManager(httpRequest)
RequestContextUtils.getInputFlashMap(httpRequest)
RequestContextUtils.getOutputFlashMap(httpRequest)

我使用了一个激烈的解决方案:直接读取会话(存储闪存属性的位置(。

CopyOnWriteArrayList<FlashMap> what = (CopyOnWriteArrayList<FlashMap>) httpRequest.getSession().getAttribute("org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS");
if (what != null) {
    FlashMap flashMap = what.get(0);
    [read flashMap as you read a HashMap]
}

我知道,这段代码非常丑陋,但目前我找不到其他解决方案。

有同样的问题,为我工作。

FlashMap flashMap = new SessionFlashMapManager((.retrieveAndUpdate(request, null(;flashMap.get("parameter"(;

最新更新