【问题标题】:Spring MVC Flash AttributesSpring MVC Flash 属性
【发布时间】:2012-08-21 23:37:27
【问题描述】:

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

POST/REQUEST/GET 实施:

  1. 客户端点击 UI 中的“复制”按钮
  2. 服务器设置响应“Location”标头
  3. 客户端重定向到“path/to/page?copy”
  4. 服务器提供 ModelAndView
  5. client(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();
}

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    我担心RedirectAttributes 只能与RedirectView 合作, 所以你的控制器应该返回例如:

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

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

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2014-02-24
      • 2017-12-18
      • 1970-01-01
      相关资源
      最近更新 更多