Spring MVC,转发 [英] Spring MVC, forward
问题描述
是否有任何区别
public class Controller1 extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new AnotherController().handleRequest(request, response);
}
}
和
@Controller
public class Controller1 {
@RequestMapping ...
public String handleRequest() {
return "forward:/path_to_my_another_controller";
}
}
推荐答案
他们'相似,但不完全相同。
They're similar, but not quite the same.
第二种方法将创建一个新的内部请求转发到第二个控制器,而第一个方法将重新使用相同的请求对象。
The second approach will create a new internal request to be forwarded on to the second controller, whereas the first one will re-use the same request object.
这是否重要取决于每个控制器对请求的作用。
Whether or not that matters depends on what each of the controllers does to the request.
我发现使用直接方法调用将控制器链接在一起是Spring注释控制器更具吸引力的方面之一,它可以比转发请求周围的请求更自然。
I've found that chaining controllers together using direct method calls is one of the more appealing aspects of Spring annotated controllers, it can make for a much more natural flow than chucking forwarded requests around.
与往常一样,您的里程可能会有所不同。
As always, your mileage may vary.
这篇关于Spring MVC,转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!