Announcement Announcement Module
No announcement yet.
View to forward to another controller? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • View to forward to another controller?

    I'm trying to write my first web application using Spring MVC and I've got a question about the pattern that I should use.

    I've got a situation where I need three pages. The first two contain forms. The last is a success/failure message and a summary of what was done. The first form gives the user a choice. Depending on their choice, the second form may be skipped (i.e., there's already enough information to take action and skip to the last page).

    Each of the pages have some text that comes from the database, so I need them all to have custom controllers (or at least something more complex than the UrlFilenameViewController) to populate the model.

    So, what I was trying to do was create three controllers, one for each page. I set out trying to use a AbstractFormController for the first two pages, but I've run into a hurdle.

    I want the controller for the third page (the status/summary page) to gather the information needed to render the view. In the case that the first controller (on submission) detects that the second page can be skipped, I want to go straight to the third controller.

    What it appears is supported and easy is to go to a view (the third JSP) from there. Well, since that's a JSP, the model won't be populated if I go straight to it. I need to hit the controller. I don't really want to replicate that model-population code in both the first and third controllers.

    So, I have a few options that I can see here.

    1. I can just use the RequestDispatcher to forward to the third controller. This is what I'm doing now and it looks ugly.

    2. I can write a new View (I think) that forwards to the named controller and return "new ModelAndView(new ForwardView(url))" from the first controller.

    What I'd like to do is find a ViewResolver that will forward to another controller if there's one with such a name. Then I could configure that resolver to run first. If it can't map the string to another controller, I can fall back on the InternalViewResolver to try to render it with a JSP.

    That would also allow me to use all the stock controllers that allow you to set the views through their properties in the XML to actually forward to another controller (SimpleFormController's successView comes to mind).

    So, am I making this really hard somehow? I don't feel like it should be hard. I feel like I'm just missing something obvious.


  • #2
    Have a look at AbstractWizardFormController.


    • #3
      forwarding to controller

      I beat my head against the wall on this for awhile, then I realized how easy it is. What I do is instead of trying to forward through the framework using a view resolver, if I want to go to another controller I just call the next controller method I want to execute (I use MultiActionControllers) ... if the controller method is in another controller class, I inject that controller into my existing controller, and call it from there.


      • #4
        Calling another controller

        How did you do that..
        I am using spring MVC
        depending on a users input if the checkbox is checked I have to display a different view which is different than the success view.
        I was able to send to a differet view with the below code but when I submit the second page it comes back to the contoller no1.
        while I want it to be take n to controller no2.
        Please help..(should I be using MultiActionControllers) or AbstractWizardFormController...
        if so any examples please..
        ModelAndView modelAndView = new ModelAndView("differentView");
        modelAndView.addObject("webSite", webSite);
        return modelAndView;

        return new ModelAndView(getSuccessView());
        Last edited by amitha123; Sep 10th, 2007, 11:09 PM.


        • #5
          Look at
          Last edited by anusis; Apr 16th, 2008, 03:37 PM.


          • #6
            All of the Spring Controllers I'm working with all return some flavor of the ModelAndView object. So, I thought, what the heck, why not just directly call one Controller from another and pass back its ModelAndView?

            Results: It actually worked. So, it appears that Spring (2.5) processes the response and renders based on what information is in the ModelAndView no matter what the Controller source. My two Controller types are an AbstractCommandController descendent calling a AbstractController descendent.

            This probably isn't documented, but works for me, so there's my caveat.


            • #7

              redirect:mywebpage.htm on the ModelAndView


              return new ModelAndView("redirect:list.htm");

              But also check MultiActionController, it is very simple to use and powerfull.