5

I am developing a Spring MVC app, and I need to check in my controller a certain condition. In case it were true, I have to return a 302 status code. It's something like this:

@RequestMapping(value = "/mypath.shtml", method = RequestMethod.GET)
public ModelAndView pageHandler(@Valid MyForm form, BindingResult result, HttpServletRequest request,
        Locale locale) throws PageControllerException, InvalidPageContextException, ServiceException {

    if (someCondition){
        // return 302 status code
    }
    else{
        // Do some stuff
    }
}

Which is the best way to do this?

Thank you very much in advance

Genzotto
  • 1,954
  • 6
  • 26
  • 45

3 Answers3

6

I finally managed to do it using @ResponseStatus, as shown here:

https://stackoverflow.com/a/2067043/2982518

UPDATE

This is the way I finally did it:

@ResponseStatus(value = HttpStatus.MOVED_TEMPORARILY)
public class MovedTemporarilyException extends RuntimeException {

    // ...
}

@RequestMapping(value = "/mypath.shtml", method = RequestMethod.GET)
public ModelAndView pageHandler(@Valid MyForm form, BindingResult result, HttpServletRequest request,
        Locale locale) throws PageControllerException, InvalidPageContextException, ServiceException {

    if (someCondition){
        throw new MovedTemporarilyException();
    }
    else{
        // Do some stuff
    }
}
Community
  • 1
  • 1
Genzotto
  • 1,954
  • 6
  • 26
  • 45
  • That will change the status in all conditions. Not just for the condition you mentioned. What I mean is that even if your condition is not true, it will still return 302 – geoand Jun 03 '14 at 11:49
  • @geoand I am only going to throw this exception in case my condition were true, otherwise I'll do whatever I need. I updated my answer to show you how. – Genzotto Jun 03 '14 at 11:51
  • Ok, I see what you are saying! Both solutions will work – geoand Jun 03 '14 at 11:54
  • 1
    how did you setup the url to send as it's a redirect? – Panthro Jul 15 '15 at 14:55
  • @Panthro I don't understand your question, could you explain it clearer? – Genzotto Jul 17 '15 at 06:20
  • When you have a redirect, the `Location` header has to be sent, how did you do it? (Sorry, it was really bad phrased) – Panthro Jul 17 '15 at 08:00
  • Check at [this](http://stackoverflow.com/questions/3318912/what-is-the-preferred-way-to-specify-an-http-location-response-header-in-sprin/15898426#15898426), I think it is what you are looking for – Genzotto Jul 18 '15 at 09:18
  • Why is HttpStatus.MOVED_TEMPORARILY now deprecated? – Luke Oct 02 '15 at 09:06
3

The following code will do:

if (someCondition){
    return new ModelAndView("redirect:" + someUrl)
}
else{
        // Do some stuff
}
geoand
  • 60,071
  • 24
  • 172
  • 190
1

org.springframework.web.servlet.view.RedirectView has a few simple parameters. One of the parameters removes the model attributes from the URL.

The previous answers worked for me, but the redirect Location header was including the model attributes added by some interceptor post methods.

return new RedirectView(newLocationVia302, true, true, false);
Sled
  • 18,541
  • 27
  • 119
  • 168
englebart
  • 563
  • 4
  • 9