Results 1 to 7 of 7

Thread: Transfer of data from one controller to another

  1. #1
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default Transfer of data from one controller to another

    This might be a stupid question, but I had to ask.

    Can anyone see any fault with this code. It will not work. In the onSubmit of Controller1 I have this code

    return new ModelAndView(new RedirectView("Controller2"), "myObject", myObject);

    In the formBackingObject of Controller2 I have this code
    MyObject myObject = (MyObject) request.getAttribute("myObject");

    I have tried sending text and objects this way, but it does not seem to get transfered from Controller1 to Controller2.

    I have also tried to explicit set it by
    request.setAttribute("myObject", myObject), but that will not work either.

    Is there something I might be missing. It should work.
    Last edited by DJViking; Feb 20th, 2008 at 08:01 AM.

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by DJViking View Post
    In the formBackingObject of Controller2 I have this code
    MyObject myObject = (MyObject) request.getAttribute("myObject");
    Should be getParameter("myObject").

    Quote Originally Posted by DJViking View Post
    I have also tried to explicit set it by
    request.setAttribute("myObject", myObject), but that will not work either.
    This can't work since it is a different request you are handling in controller 2.

    Joerg
    This post can contain insufficient information.

  3. #3
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by Jörg Heinicke View Post
    Should be getParameter("myObject").



    This can't work since it is a different request you are handling in controller 2.

    Joerg
    getParameter only returns a String not an plain object. So if I have a need to send objects from one controller to another I send it with the ModelAndView either in a map or alone. But Controller2 does not get it.

  4. #4
    Join Date
    May 2005
    Posts
    288

    Default

    Does it have to be a redirect? Use the URL of your second controller as the success view in your first controller and the second controller will receive the same request, so you can put all sorts of objects into your model.

    HTH
    Fokko

  5. #5
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by DJViking View Post
    getParameter only returns a String not an plain object. So if I have a need to send objects from one controller to another I send it with the ModelAndView either in a map or alone. But Controller2 does not get it.
    Yes, you are completely right - but that's exactly what's happening. The redirect is a directive to the browser to access the mentioned URL. An URL can only be a string. And it is a new request. These are the limitations you have to live with.

    Quote Originally Posted by F.Degenaar View Post
    Use the URL of your second controller as the success view in your first controller and the second controller will receive the same request, so you can put all sorts of objects into your model.
    What does that mean? How can the URL of the second controller be the success view of the first one? The URL is something you can type into a browser, the view is a pure name or - if resolved - an actual resource reference on the server. Whatever you try to match them the success view will not trigger the second controller to be executed - except for the redirect. But then the explanation is muddled - and the same limitations as mentioned above still apply.

    Keeping state on a server across requests is possible either by sending data via the URL (as strings obviously) or in the session. Another possibility is to avoid two requests - but then you can not execute both controllers.

    Joerg
    This post can contain insufficient information.

  6. #6
    Join Date
    May 2005
    Posts
    288

    Default

    Hi Jörg,

    forwarding to another controller by defining something like "forward:exampleAction.do" as the success view is perfectly fine with Spring MVC. Take a look at http://static.springframework.org/sp...wResolver.html and read the part about forwarding. This makes chaining controllers simple, although the isFormSubmission of the second controller has probably to be overridden.

  7. #7
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by F.Degenaar View Post
    forwarding to another controller by defining something like "forward:exampleAction.do" as the success view is perfectly fine with Spring MVC.
    You're right, I forgot about forwarding. Forwarding is a server-side redirect so that the limitations don't apply and even request attributes should work since it is the same request.

    Joerg
    This post can contain insufficient information.

Posting Permissions

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