编程语言
首页 > 编程语言> > java – Spring Tiles.如何在Controller中返回301重定向(而不是302)

java – Spring Tiles.如何在Controller中返回301重定向(而不是302)

作者:互联网

我使用的代码如下:

@RequestMapping(value="/oldPath")
public String doProductOld(
    @PathVariable(value = "oldProductId") Long oldProductId,
   Model model
) throws Exception {
    Product product = productDao.findByOldId(oldProductId);

    return "redirect:" + product.getUrlNewPath();
 }

一切正常但这个重定向返回302响应代码而不是SEO所需的301.如何轻松(没有ModelAndView或Http响应)更新它以返回301代码?

PS.当ModelAndView对象从控制器返回时我找到了解决方案,但是当返回tile alias(String)时需要Tiles的解决方案.

解决方法:

一般的想法是:

@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);
 }

标签:java,spring-mvc,spring,http-status-code-301,tiles
来源: https://codeday.me/bug/20190717/1490095.html