0

I am using spring MVC

I have a method

@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;

}

From which i am including a request to another method

@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;
}

Now how to access the model of second method from first method??

Kashif Imran
  • 579
  • 3
  • 9
  • 29

1 Answers1

0

To pass data between two controllers in a re-direct you can use custom attributes in flash scope.

Here, you can add the model from the first method in flash scope and access that in the second method.

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?

Community
  • 1
  • 1
Paul John
  • 1,626
  • 1
  • 13
  • 15
  • I can pass data from my first method to second method while forwarding by even using query params that is not a problem. The requirement is how to get model attributes of second method ie., method that is forwarded. from first method – Kashif Imran Jun 29 '15 at 12:12
  • adding model as custom flash scope attributes is the reccomended method in such scenarios. That way you would not need to add custom query parameters for this either – Paul John Jun 29 '15 at 12:13
  • First of all i cannot use redirection because i have some code that needs to be executed after the call to second method. Which is not possible using redirection as the redirection string will be the last line of the method. – Kashif Imran Jun 29 '15 at 12:16
  • I am not a Spring expert, but wouldn't a simple request attribute work here? I.e. `request.setAttribute("secondModel", model);` in the second method, and `Map modelFromSecondMethod = (Map) request.getAttribute("secondModel");` in the first method (after the `forward` call)? – Jozef Chocholacek Jun 29 '15 at 12:22
  • 1
    Also is there a reason you couldnt do the process in controller 2(method 2) in a business layer class that is called from controller 1? Do you absolutely need to call another controller? – Paul John Jun 29 '15 at 12:28
  • @JozefChocholacek I tried request.setAttribute("secondModel", model); and getAttribute technique it is not working – Kashif Imran Jun 30 '15 at 11:38
  • @PaulJohn I am working on a existing project where second controller url is dynamic. So, the answer to you question is yes i cannot do the process in controller2 in a business layer – Kashif Imran Jun 30 '15 at 11:40