This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return ModelAndView/redirect

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic