• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to return ModelAndView/redirect

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic