tags:

views:

628

answers:

2

Hi,

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

A: 

Since the redirect: prefix runs another controller, you will lose whatever you had in the ModelAndView in the previous controller. Perhaps the business logic controller should not be a controller at all -- maybe you should just make it a regular class, and have the results controller call it. Then the results controller could save the data to the model.

Kaleb Brasee
Yes, I can separate my business logic at another class. But to make this logic happen, I need input parameters. Im obtaining them from user in my first controller, and I need a way to pass them to another (result) controller, that will subsequently call business logic class. You see, my difficulty is to pass some data (at this case input parameters) between controllers (input data controller and result controller).
Oleksandr
A: 

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.

novice
But how I can save model in session using Spring-MVC with annotations. Can you give me an example?
Oleksandr
ModelAndView mv = new ModelAndView();mv.addObject("key1", "value1");mv.addObject("key2", "value2");mv.addObject("key3", "value3");request.getSession().setAttribute("model", mv.getModelMap());
novice
Thank you very much for the answer! We are almost there. The only thing I dont understand yet is how to store parametres in HttpSession with annotations.For example @SessionAttributes stores data between requests inside the controller. Is there any annotation that stores data is session permanently. Or how we can access HttpSession with @RequestParam? Ore there is some other way?
Oleksandr