Guest
Guest

Reputation: 19

How to programmatically forward one controller to another in Spring MVC 3.0

How to programmatically forward from one method in a controller to another method using spring mvc 3.0

@RequestMapping(value = "getData", method = RequestMethod.POST)
   public void getData(@RequestBody LazyTreeGridInput lazyTreeGridInput,
            HttpServletResponse response){


 if(someCondition){

    //forward to getParents


}else{
     //forward to children
   }
}

Upvotes: 1

Views: 10476

Answers (4)

sridhar
sridhar

Reputation: 303

if (someCondition) {
    return "redirect:/parentsUrl";
} else {
    return "redirect:childUrl";
}

Upvotes: 1

kevin
kevin

Reputation: 1

Try returning a String instead, and the String being the forward url.
@RequestMapping({"/getData"}) public String execute(Model model) {

if (someCondition) {
    return "forward:/parentsUrl";
} else {
    return "forward:/childUrl";
}
}

Upvotes: 0

Carlos Jaime C. De Leon
Carlos Jaime C. De Leon

Reputation: 2896

Why not put the other controller's url in the modelAndView which your handler method returns? Does calling the controller method directly the same as forwarding it?

Upvotes: 0

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

You can see here various ways to redirect using spring mvc 3.0.

UPDATE:

If you are more interested in forwarding, you should make your controllers available in this controller, and just call the method you wish to forward to.

Upvotes: 2

Related Questions