File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes How to return ModelAndView/redirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to return ModelAndView/redirect" Watch "How to return ModelAndView/redirect" New topic
Author

How to return ModelAndView/redirect

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, I am having a request handler which receives form data. For example say user id and password. I will validate this data with the database. If the credentials are invalid then I will show the login form. If the credentials are valid then I will show the home page.

If the credentials are invalid then I want to send some error messages. Here I have to create a ModelAndView object add the error messages to the ModelAndView object and return the ModelAndView.

If the credentials are valid I have to show the home page. As you all know we should not use ModelAndView in this case. Because it is a forward. So user id and password will also be forwarded to the home page, which I dont want to do. So here i want to do a redirect. So I use "redirect:home.do"

How should I handle such kind of return types? Should I simple put Object as return type. Can spring recognize the returned type and do a forward or redirect accordingly?

Can anyone please tell me. Thank you in advance.


Love all, trust a few, do wrong to none.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

It sounds like you are doing something that Spring Security can do for you. Putting that aside you can do something like this (Assumes Spring version 3.1):




After the redirect, flash attributes are automatically added to the model of the controller that serves the target URL.


[How To Ask Questions][Read before you PM me]
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi, I wrote this in my controllerI could not see any flash attributes being added. In my home.jsp page when I try to print the flash attribute it is not able to print. I used thisShould I configure some kind of view resolver. Right now I am using InternalResourceViewResolver

PS: I am using Spring 3.1 only
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

What you have will work, I use it all the time. However if you refresh with f5 or hit the /home URL directly it will be null. This is because Flash Scope supports only one redirect and its attributes are automatically cleared after redirect.

If this is for a post redirect get pattern then that is kind of important. Since both these handler methods are in the same controller you could get away with just using @SessionAttribute. The approach you are using will work as well but you will have to do something like this to handle the cases I mentioned.

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Just another thing, you could do this as well




The model attribute will be pulled off of your flashmap and passed in. Of course this won't work if the attribute is null. You could introduce an @SessionAttributes("loggedin") on the controller though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return ModelAndView/redirect