简体   繁体   English

如何在ModelAndView中使用Redirect

[英]How to use Redirect in ModelAndView

I Want to redirect my url to http://localhost:8080/ClickBuy/product/details to http://localhost:8080/ClickBuy/home . 我想将我的网址重定向到http:// localhost:8080 / ClickBuy / product / detailshttp:// localhost:8080 / ClickBuy / home

I have Define header.jsp and Include it in all my Pages.When I click home link from anywhere else url add /home from current url.So it shows 404 error. 我已经定义了header.jsp并将其包含在我的所有Pages中。当我从其他任何地方点击主页链接url add / home从当前url.So它显示404错误。

Controller 调节器

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

How can I use redirect:/ in ModelAndView return type method??? 如何在ModelAndView中使用redirect:/返回类型方法???

To Use redirect:/ in ModelAndView return type method, you are try following 要在ModelAndView返回类型方法中使用重定向:/,请尝试以下操作

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

You can also return home page using String returntype if you do not need any Model in home page 如果您不需要主页中的任何模型,也可以使用String returntype返回主页

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

There are different ways to redirect but using Spring ModelView Object is simple 有不同的重定向方法,但使用Spring ModelView Object很简单

@RequestMapping(value = "/redirecturl", method = RequestMethod.POST)
    public ModelAndView methodPost() {
            return new ModelAndView( "redirect:/login.html");

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM