Spring MVC,向前
问题内容:
之间有什么区别
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";
}
}
问题答案:
它们相似,但不完全相同。
第二种方法将创建一个新的内部请求,将其转发到第二个控制器,而第一种将重用同一请求对象。
是否重要取决于每个控制器对请求执行的操作。
我发现使用直接方法调用将控制器链接在一起是Spring带注释的控制器更吸引人的方面之一,它比顺带处理转发的请求要自然得多。
和往常一样,您的里程可能会有所不同。