Spring 3.0转发请求到不同的控制器 [英] Spring 3.0 forwarding request to different controller

查看:111
本文介绍了Spring 3.0转发请求到不同的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在春季将请求转发给其他控制器的正确方法是什么?

What is the proper way to forward a request in spring to a different controller?

@RequestMapping({"/someurl"})
public ModelAndView execute(Model model) {
    if (someCondition) {
        //forward to controller A
    } else {
        //forward to controller B
    }
}

所有控制器都有Spring注入的依赖项,所以我不能只创建它们并自己调用它们,但我希望将请求属性传递给其他控制器。

All of the controller have dependencies injected by Spring, so I can't just create them and call them myself, but I want the request attributes to be passed on to the other controllers.

推荐答案

尝试返回一个String,而String则是前向url。

Try returning a String instead, and the String being the forward url.

@RequestMapping({"/someurl"})
public String execute(Model model) {
    if (someCondition) {
        return "forward:/someUrlA";
    } else {
        return "forward:/someUrlB";
    }
}

这篇关于Spring 3.0转发请求到不同的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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