aspose file tools*
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 Spring in Action this week in the Spring 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
Author

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

kirk israel
Greenhorn

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

Thanks!
kirk israel
Greenhorn

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

According to http://jira.springframework.org/browse/SPR-5468 it seems like sometime soonish I should be able to write

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

but not yet.

http://forum.springsource.org/archive/index.php/t-16370.html tells me I should be able to extend RedirectView and override sendRedirect with:
response.setStatus(301);
response.setHeader("Location", response.encodeRedirectURL(targetUrl));

but for now my lead just says

response.setStatus(301);
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.

Thoughts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ModelAndView: redirect as 301 (Permanent) not 302 (Temp)?