Announcement Announcement Module
Collapse
No announcement yet.
Transfer of data from one controller to another Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 08:01 AM.

  • #2
    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").

    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

    Comment


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

      Comment


      • #4
        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

        Comment


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

          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

          Comment


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

            Comment


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

              Comment

              Working...
              X