Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am redirecting from a controller to another controller. But I also need to pass model attributes to the second controller.

I don't want to put the model in session.

Please help.

like image 378
ashishjmeshram Avatar asked Sep 15 '11 10:09

ashishjmeshram


People also ask

What is the use of model Addattribute () in spring?

Furthermore, there's also an addAttributes() method. Its purpose is to add values in the Model that'll be identified globally. That is, every request to every controller method will return a default value as a response.

What is ModelAttribute?

@ModelAttribute can be used as the method arguments / parameter or before the method declaration. The primary objective of this annotation to bind the request parameters or form fields to an model object.

What is the scope of controller in Spring MVC?

To answer your first question: yes, Spring MVC controllers are singletons by default. An object field will be shared and visible for all requests and all sessions forever. However without any synchronization you might run into all sorts of concurrency issues (race conditions, visibility).


2 Answers

I use spring 3.2.3 and here is how I solved similar problem.
1) Added RedirectAttributes redirectAttributes to the method parameter list in controller 1.

public String controlMapping1(         @ModelAttribute("mapping1Form") final Object mapping1FormObject,         final BindingResult mapping1BindingResult,         final Model model,          final RedirectAttributes redirectAttributes) 

2) Inside the method added code to add flash attribute to redirectAttributes redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);

3) Then, in the second contoller use method parameter annotated with @ModelAttribute to access redirect Attributes

@ModelAttribute("mapping1Form") final Object mapping1FormObject 

Here is the sample code from Controller 1:

@RequestMapping(value = { "/mapping1" }, method = RequestMethod.POST) public String controlMapping1(         @ModelAttribute("mapping1Form") final Object mapping1FormObject,         final BindingResult mapping1BindingResult,         final Model model,          final RedirectAttributes redirectAttributes) {      redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);      return "redirect:mapping2"; }    

From Contoller 2:

@RequestMapping(value = "/mapping2", method = RequestMethod.GET) public String controlMapping2(         @ModelAttribute("mapping1Form") final Object mapping1FormObject,         final BindingResult mapping1BindingResult,         final Model model) {      model.addAttribute("transformationForm", mapping1FormObject);      return "new/view";   } 
like image 62
aborskiy Avatar answered Sep 21 '22 18:09

aborskiy


Using just redirectAttributes.addFlashAttribute(...) -> "redirect:..." worked as well, didn't have to "reinsert" the model attribute.

Thanks, aborskiy!

like image 39
pkopac Avatar answered Sep 18 '22 18:09

pkopac