spring3中的重定向
在Spring 3中,您可以简单地映射URL,如下所示:spring3中的重定向,spring,spring-mvc,Spring,Spring Mvc,在Spring 3中,您可以简单地映射URL,如下所示: @RequestMapping(value = "/index.html", method = RequestMethod.GET) public String index(Model model) { return "index"; } 是否可以使这种方法重定向到另一个url,如: @RequestMapping(value = "/index.html", method = RequestMethod.GET) publi
@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index(Model model) {
return "index";
}
是否可以使这种方法重定向到另一个url,如:
@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";
}
您不需要重定向-只需调用以下方法:
@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";
}
是的,重定向将起作用。在index方法中,将最后一行更改为
返回“redirect:/second.html”代码>
编辑
需要上下文路径和控制器映射。如果DispatcherServlet映射到/ABC,控制器的请求映射为/XYZ,则您必须写入:
返回“重定向:/ABC/XYZ/second.html”代码>我认为您的方法存在安全漏洞。如果index()具有isAnonymous()授权,second()具有“已验证”授权,该怎么办?直接从index()调用second()会不会导致糟糕的情况?更糟糕的是,现在对受保护的second()的调用来自内部方法,而不是外部请求,因此在调用second()时,“信任”因子会高得多?@skaffman但如果控制器方法返回ResponseEntity(例如),但如果控制器方法返回ResponseEntity(例如)?