Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kirk israel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic