views:

259

answers:

2

Is there any difference between

public class Controller1 extends AbstractController {
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        return new AnotherController().handleRequest(request, response);
    }
}

and

@Controller
public class Controller1 {

    @RequestMapping ...
    public String handleRequest() {
        return "forward:/path_to_my_another_controller";
    }
}
+1  A: 

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.

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.

skaffman
A: 

By creating controllers yourself, you will prevent Spring from injecting any dependencies into them. This may cause your self-created controllers to not work correctly.

If you really need to chain controllers like this, I would ask the Spring application context for an instance of the controller you want instead of creating one with the new operator.

davetron5000