如何在ModelAndView中使用重定向


问题内容

我想将我的网址重定向到http:// localhost:8080 / ClickBuy / product /
details
http://
localhost:8080 / ClickBuy / home

我已经定义了header.jsp并将其包含在我的所有页面中。当我单击其他任何位置的home链接时,请从当前URL添加/ home。因此它显示404错误。

控制者

public ModelAndView allProduct()
{   
ModelAndView model=new ModelAndView("pl");
model.addObject("data", pd.getAllProducts());
return model;
}

我如何在ModelAndView返回类型方法中使用redirect:/?


问题答案:

若要在ModelAndView返回类型方法中使用redirect:/,请尝试以下操作

ModelAndView modelAndView =  new ModelAndView("redirect:/abc.htm");
modelAndView.addObject("modelAttribute" , new ModelAttribute());
return modelAndView;

如果您不需要主页中的任何模型,也可以使用String returntype返回主页。

@RequestMapping(value = "/home.htm", method = RequestMethod.GET)
   public String homePage() {
      return "home";
   }