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?
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
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.
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.