Java 在调用DispatcherServlet之前,如何访问筛选器链中的spring mvc flash redirectAttribute?
我有以下控制器:Java 在调用DispatcherServlet之前,如何访问筛选器链中的spring mvc flash redirectAttribute?,java,spring-mvc,redirect,post-redirect-get,Java,Spring Mvc,Redirect,Post Redirect Get,我有以下控制器: @Controller @RequestMapping("/my-account") public class AccountController { @RequestMapping(value = "/foo/post", method = RequestMethod.POST) public String doPost(final RedirectAttributes redirectAttributes) { re
@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
,该过滤器链最终调用springs defaultDispatcherServlet
,后者反过来调用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
实例被私有地保存在dispatcher servlet中
考虑到所有这些方法都将
null
返回到我的过滤器中(我不明白为什么),有人知道如何在过滤器链中为继POST
之后的GET
请求访问重定向属性吗
我使用了一个极端的解决方案:直接将数据读入会话(存储flash属性的地方)
CopyOnWriteArrayList what=(CopyOnWriteArrayList)httpRequest.getSession().getAttribute(“org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_映射”);
if(what!=null){
FlashMap FlashMap=what.get(0);
[阅读HashMap时阅读flashMap]
}
我知道,这段代码非常难看,但目前我还没有找到其他解决方案。也有同样的问题,下面的代码对我有用 FlashMap FlashMap=new SessionFlashMapManager().retrieveAndUpdate(请求,null);
flashMap.get(“参数”) 我正在为同样的问题寻找解决方案!如果我找到一些东西,我会写。只有当
DispatcherServlet
将其FlashMapManager
应用于请求时,使用RequestContextUtils
才会提供结果。使用what.get(0)
的问题是,如果同一个http会话有多个挂起的重定向,则此操作可能会失败。代码工作起来很有魅力。谁在乎它是不是超级丑?:)
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]
}