This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Spring and the fly likes ModelAndView: redirect as 301 (Permanent) not 302 (Temp)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "ModelAndView: redirect as 301 (Permanent) not 302 (Temp)?" Watch "ModelAndView: redirect as 301 (Permanent) not 302 (Temp)?" New topic

ModelAndView: redirect as 301 (Permanent) not 302 (Temp)?

kirk israel

Joined: May 28, 2009
Posts: 27
So, I'm still kind of new to Spring, having done a good amount of work on it, but never started much from scratch, so I might be missing something obvious

I believe that this line of code:
return new ModelAndView("redirect:" + getNewURL());

is what is responsible for doing a 302 (Moved Temporarily) redirect .

i don't know if this is kind of a "built in" to Spring behavior or if there's some kind of configuration that gets that to work, but I need to get a similar redirection going as a 301 (Moved Permanently) redirect, since we believe that search engines prefer that.

I'm not sure where to find out how the redirection is supposed to be setup, what I would have to do to get the HTTP headers right for the other kind of redirect... it looks like I'm pretty close to the top of "our" program stack, where Spring etc's Filter Chain kicks in, but i could be missing something...

kirk israel

Joined: May 28, 2009
Posts: 27
Ok, so it seems like that whole
thing is just a builtin shortcut, I assume the view in question is a RedirectView()

According to it seems like sometime soonish I should be able to write

ModelAndView redirectModelAndView = new ModelAndView();
RedirectView redirectView = new RedirectView(brandService.getBaseHref(request));
return redirectModelAndView;

but not yet. tells me I should be able to extend RedirectView and override sendRedirect with:
response.setHeader("Location", response.encodeRedirectURL(targetUrl));

but for now my lead just says

response.setHeader("Location", brandService.getBaseHref(request));
response.setHeader("Connection", "close");
return null;

and Spring will take that null ModelAndView and just let the other stuff pass through.

I agree. Here's the link:
subject: ModelAndView: redirect as 301 (Permanent) not 302 (Temp)?