Announcement Announcement Module
Collapse
No announcement yet.
best way to pass commands between controllers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • best way to pass commands between controllers

    Hello everyone,
    I am a bit new to the whole spring framework, but am really getting excited about what I have been finding in this framework. I have a question about what the best practices are in regards to passing data between controllers. Here is my situation:

    I have two controllers: SearchController and ResultsController.
    In my SearchController's onSubmit method I take all data from my SearchCommand object and pass it to a service which returns data to be put into a new ResultsCommand. So at this time, what should I do if I want my ResultsController's formBackingObject method to grab the command?

    Do I put the newly created ResultsCommand in session and grab it from there? Or, is there a "Spring" way of doing this? I know that onSubmit() returns a ModelAndView which has a constructor which takes (View v, String objectName, Object object)… so could I return new ModelAndView(getSuccessView(),getCommandName(),res ultsCommandObject) from the onSubmit method…(resultsCommandObject is the newly built ResultsCommand). If this works, how would I get the command in the formBackingObject() on the other side?

    Is there a better way to do things than these two or am I missing something?

    Any help is greatly appreciated,
    Ryan

  • #2
    In my SearchController's onSubmit method I take all data from my SearchCommand object and pass it to a service which returns data to be put into a new ResultsCommand. So at this time, what should I do if I want my ResultsController's formBackingObject method to grab the command?
    I'm not sure of the need for a ResultsController class -- you just want to display your search results, correct? If so make up a view that will display them -- in your onSubmit() method return a ModelAndView with that view and the model (results) data you've found.

    Comment


    • #3
      yeah, but there are more cases where I need to do it other than this example.

      Comment


      • #4
        I agree that you don't need a Result Controller.

        Usually you can just use the SimpleFormController for search-result type pages.

        If you need more complex behaviour, use MultiActionFormControllers. Spring doesn't provide such a implementation, but a lot of us have posted various flavours of implementation on the forum just search around.

        I would say abt 90% you won't need to. So before you do as such, consider and try to use a cleaner solution as mentioned that will do away with the need to pass data to another controller.

        for the 10% of the time where there's really no other way, you could pass it thru the data as hidden fields to the next controller if its just some simple data. usually this is too troublesome unless data is real simple.

        Otherwise, just put it in session. You need to use WebUtils.setSessionAttribute() and getSessionAttribute.

        Comment


        • #5
          There could be a time when you face a problem like mine here:

          You have a search page that display the result right under the search criteria (just like yahoo search page). There are indeed 2 jsp page: a master page named search.jsp, and the result page (which would be included in the search.jsp) named result.jsp. The bean object that is processed by the controller will be used in result.jsp.

          Then you have a button that print the search result. One simpliest solution is to place a button that have an action to invoke window.print() java script so it will print the hole page on your browser (include all the frames, and even the search cirteria fields) so that's s...cks ^_^.

          The second solution is to use another result controller that will receive the bean object from the search controller, and display the result only on the result.jsp page.

          However I still not be able to pass the bean object from the search controller to the result controller.

          any idear???

          Comment

          Working...
          X