在Spring 3中,您可以像这样简单地映射网址:
@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model) {
return "index";
}
是否有可能使这种方法有点重定向到另一个网址,如:
@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model) {
return "second.html";
}
@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model) {
//put some staff in model
return "second";
}
答案 0 :(得分:15)
您无需重定向 - 只需调用方法:
@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model) {
return second(model);
}
@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model) {
//put some staff in model
return "second";
}
这是注释风格的好处之一;你可以把你的方法连在一起。
如果确实想要重定向,那么您可以将其作为视图返回:
@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public View index(Model model) {
return new RedirectView("second.html");
}
@RequestMapping(value = "/second.html", method = RequestMethod.GET)
public String second(Model model) {
//put some staff in model
return "second";
}
答案 1 :(得分:6)
是重定向是否有效。在索引方法中,将最后一行更改为return "redirect:/second.html" ;
修改强>
上下文路径和控制器映射是必需的。如果DispatcherServlet映射到/ ABC并且控制器的请求映射是/ XYZ,那么您将必须写:
return "redirect:/ABC/XYZ/second.html";