Spring MVC,转发 [英] Spring MVC, forward

查看:123
本文介绍了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";
    }
}


推荐答案

他们'相似,但不完全相同。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆