Results 1 to 7 of 7

Thread: View to forward to another controller?

  1. #1
    Join Date
    Jul 2005
    Posts
    1

    Default 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.

    Thanks,
    -Justin

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Have a look at AbstractWizardFormController.

  3. #3
    Join Date
    Aug 2007
    Posts
    14

    Default 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. #4
    Join Date
    Sep 2007
    Posts
    1

    Default Calling another controller

    Hey
    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..
    if(dataBean.getIncludeDetails())
    {
    ModelAndView modelAndView = new ModelAndView("differentView");
    modelAndView.addObject("webSite", webSite);
    return modelAndView;


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

  5. #5
    Join Date
    Jan 2008
    Posts
    25

    Default

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

  6. #6
    Join Date
    Jun 2006
    Posts
    6

    Default

    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. #7

    Lightbulb Solution

    use
    redirect:mywebpage.htm on the ModelAndView

    Example:

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

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

Similar Threads

  1. Replies: 5
    Last Post: Feb 3rd, 2011, 05:18 AM
  2. Replies: 9
    Last Post: Nov 1st, 2005, 10:36 PM
  3. Replies: 3
    Last Post: Sep 22nd, 2005, 10:14 AM
  4. Content Provider vs View Model
    By Martin Kersten in forum Swing
    Replies: 21
    Last Post: Mar 10th, 2005, 02:25 PM
  5. Replies: 2
    Last Post: Sep 20th, 2004, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •