如何增强下面的 Controller 以利用 Spring MVC 的 Flash 属性?该用例是复制函数。
POST/REQUEST/GET 实现:
- 客户端点击用户界面中的“复制”按钮
- 服务器设置响应“位置” header
- 客户端重定向到“path/to/page?copy”
- 服务器提供ModelAndView
- 客户端(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();
}
最佳答案
我担心 RedirectAttributes
仅适用于 RedirectView
,
所以你的 Controller 应该返回例如:
1. String: "redirect:/[uriPath]"
2. new RedirectView([uriPath])
如果您确实需要使用 JS 处理服务器响应,那么处理 HTTP 状态 302 可能会有所帮助。
关于java - Spring MVC Flash 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12064547/