Spring + Tiles. How to return 301 redirects (instead of 302) to controller
I am using code like:
@RequestMapping(value="/oldPath")
public String doProductOld(
@PathVariable(value = "oldProductId") Long oldProductId,
Model model
) throws Exception {
Product product = productDao.findByOldId(oldProductId);
return "redirect:" + product.getUrlNewPath();
}
Everything works fine, but this redirect returns a 302
response code instead of 301
which is needed for SEO. How easy is it (without ModelAndView
or Http response) to update it to get the 301
code back ?
PS. I found solutions when the returned object was ModelAndView
returned from the controller, but the solution Tiles
requires a solution when an alias (String) is returned.
+3
source to share
4 answers
General idea:
@RequestMapping(value="/oldPath")
public ModelAndView doProductOld(
@PathVariable(value = "oldProductId") Long oldProductId,
Model model
) throws Exception {
Product product = productDao.findByOldId(oldProductId);
RedirectView red = new RedirectView(product.getUrlNewPath(),true);
red.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
return new ModelAndView(red);
}
+3
source to share