java - Spring MVC Flash 属性

标签 java spring spring-mvc

如何增强下面的 Controller 以利用 Spring MVC 的 Flash 属性?该用例是复制函数。

POST/REQUEST/GET 实现:

  1. 客户端点击用户界面中的“复制”按钮
  2. 服务器设置响应“位置” header
  3. 客户端重定向到“path/to/page?copy”
  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();
}

最佳答案

我担心 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/

相关文章:

java - 如何导入spring配置?

java - 如何使用 Spring Boot 通过扇出交换在 RabbitMQ 上发布消息

java - 如何在 Spring MVC 中使用单个 View 执行所有 CRUD 操作?

java - 如何在 Spring 4.2 或更高版本中设置 Cache-control : private with applicationContext. xml

java - 如何找出网络服务器显示的默认页面的名称?

java - 删除 Oracle Java SE 的商业组件

java - 如何在 log4j2 中创建自定义 Appender?

Java:在套接字编程中从InputStream接收不同类型的文件

java - 如何在spring mvc中使用带有freemarker的消息?

Spring MVC RequestMapping 匹配错误的 URL