1

如何增强下面的控制器以利用 Spring MVC 的 Flash 属性?用例是一个复制功能。

POST/REQUEST/GET 实现:

  1. 客户端单击 UI 中的“复制”按钮
  2. 服务器设置响应“位置”标头
  3. 客户端重定向到“路径/到/页面?复制”
  4. 服务器提供 ModelAndView
  5. 客户端(jQuery成功函数)设置window.location

FooController 重定向方法:

@RequestMapping(value = "{fooId}", method = POST, params = { "copy" })
@Transactional
@ResponseStatus(CREATED)
public void getCopyfoo(@PathVariable String fooId, 
HttpServletResponse response, RedirectAttributes redirectAttrs) {
    response.setHeader("Location", uriPath);
    //no worky?!:
    redirectAttrs.addFlashAttribute("barKey", "barValue");
}

FooController 获取方法:

@RequestMapping(value = "{fooId}", method = GET)
@Transactional(readOnly = true)
public ModelAndView findFooById(@PathVariable String fooId, 
HttpServletRequest request){ 
    Map<String, ?> map = RequestContextUtils.getInputFlashMap(request);
    // map is empty...
    return modelAndViewFromHelperMethod();
}
4

1 回答 1

0

我很害怕RedirectAttributes它只适用于RedirectView,所以你的控制器应该返回例如:

1. String: "redirect:/[uriPath]"
2. new RedirectView([uriPath])

如果你真的需要用 JS 处理服务器响应,那么处理 HTTP 状态 302 可能会有所帮助。

于 2012-11-15T23:25:42.557 回答