如何在Spring上从控制器重定向到另一个控制器

时间:2013-04-30 09:28:18

标签: spring spring-mvc

尝试做一些非常容易的事情...... 我使用Spring MVC 3,我想从另一个控制器内部重定向一个控制器... ex:

@Controller
@SessionAttributes
public class UserFormController {
@Autowired
private UserService userService;
@RequestMapping(value = "/method1", method = RequestMethod.GET)
public ModelAndView redirectFormPage() {
//redirect to controller2 here, 
//pointing to a method inside it, without going to any url first
}

试图用modelandview做到这一点,但在我看来,所有的解决方案都必须首先通过网址,然后才能重定向到控制器。

感谢所有的帮助!

===============更多信息=========== 流程是这样的:控制器1上的methodA被调用...做一些事情......然后想要将用户重定向到listPage ...这个页面有一个对象列表,控制器2上的methodB能够​​加载并将其发送到此listPage。我想要实现的是:总是有人需要获取此页面,我将在此控制器2上调用此方法并加载它。

3 个答案:

答案 0 :(得分:1)

你可以做到

public String redirectFormPage() {
    return "redirect:/url/controller2";
}

答案 1 :(得分:1)

我不知道你为什么要这样做,但这可能有效:

@Controller
@SessionAttributes
public class UserFormController {
@Autowired
private UserService userService;

@Autowired
private Controller2 controller2;

@RequestMapping(value = "/method1", method = RequestMethod.GET)
public ModelAndView redirectFormPage() {
 return controller2.redirectMethod();
}

只需注入Controller2并调用所需的方法

答案 2 :(得分:0)

好问题。我喜欢使用struts重定向操作结果类型,以避免将下一个视图的知识放在处理已发布表单的操作中。

例如,类似UpdateContactInfoAction的类的操作方法将返回contactInfoSaved,而不是为下一个视图准备数据并返回gotoShippingOptionsPage。知道流程的下一步是选择送货选项只在struts配置中。

任何人都知道如何使用Spring MVC做到这一点?

相关问题