Announcement Announcement Module
Collapse
No announcement yet.
onSubmit - Passing an object to another controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • onSubmit - Passing an object to another controller

    Hey,

    Im relatively new to using Spring MVC and I was hoping that someone could help me clear up a problem that I am having.

    At the moment I have a form which takes in certain details. The onSubmit method in this controller redirects to a second form controller. I need the command object from Controller1 to use in Controller2.

    At the moment I am adding the object to the session in the onSubmit of Controller one and then retrieving it in Controller2. Is this good practice or is there a better way of doing it? Any help is very much appreciated.

  • #2
    Passing an object from one controller to another

    Hi i am also facing the same issue.Any help will be appreciated.
    thanks
    Mayank

    Comment


    • #3
      A wizard controller maybe...

      Comment


      • #4
        Advice

        It depends upon what you are trying to accomplish. Are you building a wizard style page flow, if so then you might want to look at Spring WebFlow.

        Can you tell us a little bit more about what you are trying to accomplish?

        Comment


        • #5
          If you are using a SimpleFormControllers here is what you can do. ( I say you can, but not sure if its good or not )
          Use forward action in controller1, then the model should be available in controller2 in request scope.

          Controller 1
          public ModelAndView onSubmit(Object command) throws ServletException {
          .....
          return new ModelAndView("forward:Controller2.do","mydata",dat aObj);
          }

          Controller 2
          protected Object formBackingObject(HttpServletRequest request) throws Exception {
          Object forwardedObj = request.getAttribute("mydata");
          // if flow comes from Another controller with forward: action,
          // the model gets passed as request attribute. so use that.
          // But on subsequent sumits within the same page, this request attribute is not present.
          // Follow your logic for normal form submit as expected by spring life cycle.
          MyClass mData = null;
          if( forwardedObj != null ){
          mData = (MyClass)forwardedObj;
          } else {
          mData = new MyClass();
          // or you might simply call
          // return super.formBackingObject(request);
          }
          return mData;
          }
          Last edited by maddy; Sep 3rd, 2008, 02:15 PM.

          Comment

          Working...
          X