Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - pass model between controllers

Tags:

spring-mvc

I have created a controller that does some business logic and creates a model. If I pass this model directly to view by returning ModelAndView with view name and model - everything working great. But now I want to display results at another page. So I use "redirect:" prefix to redirect to another controller, but the model is lost.

What Im missing?

Regards, Oleksandr

like image 746
Oleksandr Avatar asked Jan 01 '10 16:01

Oleksandr


3 Answers

you can use the forward: prefix which ultimately does a RequestDispatcher.forward() insted of The redirect: prefix.

like image 173
Niraj Sonawane Avatar answered Nov 09 '22 11:11

Niraj Sonawane


Option 1 : You might put the model in session and get it back in the controller and nullify it in session.

Option 2 : You said, you are having two controllers, first one would retrieve the user input and do some business logic and redirect to other one. My suggestion is to move the business logic which is placed in both controllers to a class and have only one controller which would return the model and view to the user.

like image 25
Maniganda Prakash Avatar answered Nov 09 '22 11:11

Maniganda Prakash


During redirect: request is sent using GET method with parameters appended to the url. You can right a new method in the controller to receive the request parameters with @RequestParameter annotation. While redirecting the request simple add all the parameters as string

e.g ModelAndView mv = new ModelAndView();
mv.setViewName(redirect:/your/url);
mv.addObject("param1", string_param);
.
.
.

This way you can redirect successfully.

like image 22
Adiant Avatar answered Nov 09 '22 10:11

Adiant