java 如何在 ModelAndView 中使用重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43079276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Redirect in ModelAndView
提问by Mohanraj
I Want to redirect my url to http://localhost:8080/ClickBuy/product/detailsto http://localhost:8080/ClickBuy/home.
我想将我的 url 重定向到http://localhost:8080/ClickBuy/product/details到 http://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 并将它包含在我所有的页面中。当我从其他任何地方单击主页链接时,从当前 url 添加 /home。所以它显示 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 返回类型方法中使用重定向:/????
回答by Neha Shettar
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";
}
回答by Sulaha
There are different ways to redirect but using Spring ModelView Object is simple
有多种重定向方式,但使用 Spring ModelView 对象很简单
@RequestMapping(value = "/redirecturl", method = RequestMethod.POST)
public ModelAndView methodPost() {
return new ModelAndView( "redirect:/login.html");
}