6

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

alexbt
  • 16,415
  • 6
  • 78
  • 87
Oleksandr
  • 61
  • 1
  • 1
  • 2

5 Answers5

11

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

Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
4

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.

Maniganda Prakash
  • 4,702
  • 8
  • 34
  • 42
  • 1
    But how I can save model in session using Spring-MVC with annotations. Can you give me an example? – Oleksandr Jan 02 '10 at 13:40
  • 4
    ModelAndView mv = new ModelAndView(); mv.addObject("key1", "value1"); mv.addObject("key2", "value2"); mv.addObject("key3", "value3"); request.getSession().setAttribute("model", mv.getModelMap()); – Maniganda Prakash Jan 02 '10 at 13:58
  • 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 Jan 02 '10 at 15:10
  • @Oleksandr, check out http://forum.springsource.org/showthread.php?t=47185 for more information as I don't think @novice truely answered your question. – Chris Thompson Feb 11 '11 at 15:55
2

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.

Adiant
  • 859
  • 4
  • 16
  • 34
0

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
  • 51,193
  • 8
  • 108
  • 113
  • 1
    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 Jan 01 '10 at 16:26
0

i would not use @SessionAttributes as it may change in future releases of spring. I would stick with the old fashioned way that Oleksandr showed you above

somehow an old post but maybe someone else will find it usefull

giannisapi
  • 2,641
  • 3
  • 24
  • 31