如何从先前方法包含的方法中获取模型值

时间:2015-06-29 11:12:54

标签: spring-mvc servlets requestdispatcher

我正在使用spring MVC

我有一个方法

@RequestMapping(value = "/firstMethod/getDetails", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getFirstMethodDetails(HttpServletRequest request,
        HttpServletResponse response) {
 Map<String, Object> model = new HashMap<String, Object>();
 model.put("name", "Bryan");
 request.getRequestDispatcher("/secondmethod/getDetails")
                    .include(request, response);

 // want to access second methods model values here    

 return model;

}

我将请求包含在另一种方法中

@RequestMapping(value = "/secondMethod/getDetails", method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getSecondMethodDetails(HttpServletRequest request,
        HttpServletResponse response) {
 Map<String, Object> model = new HashMap<String, Object>();
 model.put("age", 29);
 return model;
}

现在如何从第一种方法访问第二种方法的模型??

1 个答案:

答案 0 :(得分:0)

要在重定向中的两个控制器之间传递数据,您可以在闪存范围中使用自定义属性。

在这里,您可以从闪存范围中的第一个方法添加模型,并在第二个方法中访问该模型。

http://java.dzone.com/articles/spring-mvc-flash-attributes

@RequestMapping(method = RequestMethod.POST)

  public String handleFormSubmission(..., final RedirectAttributes     redirectAttrs) {
    ...
    redirectAttrs.addFlashAttribute("AttributeName", value);
    return "redirect:to_some_url_handled_by_BController";
}

SO post - How to pass model attributes from one Spring MVC controller to another controller?