Announcement Announcement Module
No announcement yet.
Controller forwaring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Controller forwaring


    after reading tons of documentation I'm trying to write my first spring web app. I came to a point where my documentations and books don't help me a lot.

    I have an 'overview' page where someone can select an item of a list (select-box). There are several submit buttons on this page - 'New', 'Edit', 'Delete'. The form action is the same Controller that is responsible for showing (read: returning ModelAndView for) this overview page. So, soon a submit button is pressed, the controller is called and should forward to an other controller - if the button 'Edit' was pressed the controller should forward to an edit-controller (with the id to load), if the button 'Delete' was pressed, then the controller should forward to the delete-controller, and so on.

    The overview controller just implements the Controller interface and so the handleRequest() method. The Edit-Controler extends the SimpleFormController and overrides the formBackingObject() and onSubmit() methods.

    Is this the way to go, or would you implement it differently? I don't understand how to pass the id to edit for example to the edit-controller.

    Can you give me a hint in the right direction?


  • #2
    Not sure why you are using a controller to handle the navigation. If it is really complicated use springwebflow. If there is no state then don't bother having a controller, rather map each action to it's own controller, i.e. EditController mapped to /edit.htm, DeleteController mapped to /delete.htm.

    Then your navigation jsp just submits (or links) to edit.htm?id=${someId}

    If you want to get hold of that id, i.e. bind it to a Form, then in your controllers set bindOnNewForm to true.

    Also; "The form action is the same Controller" doesn't actually make any sense "form action" has no meaning in Spring (although they do in webflow). The "form bean" is the bean which request parameters are introspected into, i.e. a data holder, and your controller does something with that bean.

    Hope this helps.



    • #3
      Thanks for your help, yatesco. Maybe I need some further explaination...

      Originally posted by yatesco
      If there is no state then don't bother having a controller, rather map each action to it's own controller, i.e. EditController mapped to /edit.htm, DeleteController mapped to /delete.htm.
      This is exactly, what I want to do.

      Originally posted by yatesco
      Then your navigation jsp just submits (or links) to edit.htm?id=${someId}
      That is the problem. I don't have a navigation for the items - it is a select box. Something like that:

      <form method="post" action="whatever.htm">
          <select name="id">
              <option value="1">entry #1</option>
              <option value="2">entry #2</option>
          <input type="submit" name="submit" value="Edit"/>
          <input type="submit" name="submit" value="Delete"/>
      When the Edit button is pressed, I want to execute the EditController, when the Delete button is pressed I want to execute the DeleteController.

      The whatever.htm (the controller behind it) has to forward/redirect/whatever the request depending on the submit button pressed.


      • #4
        Maybe adding an HandlerInterceptor is the way to do, but I'm not sure.


        • #5
          Never used one, but have a look at MultiActionController?

          Alternatively have a controller which takes in a map of key:viewNames, and then have your select box values be the key. The your controller simply looks up the view for that controller and forwards/redirects to that view.

           class  NavigationController extends AbstractController &#123;
             private final Map navigationMap;
             public NavigationController&#40;final Map theNavigationMap&#41; &#123;
               this.navigationMap = theNavigationMap;
             public final void whatEverMethodItIsThatYouImplement&#40;final HttpServletRequest request, final HttpServletResponse response&#41; &#123;
               String viewName = RequestUtils.getRequiredRequestParameter&#40;"viewName"&#41;;
               String view = navigationMap.get&#40;viewName&#41;;
               return new ModelAndView&#40;view, request.getParametersMap&#40;&#41;&#41;;
          I am doing this from home but you get the idea.

          NOTE: whenever I write code like this there is always a nagging feeling that "there must be a better way". And if there is one thing I have learnt, with Spring, THERE IS


          • #6
            if you got validations happening for each action you perform, then multiactioncontroller might not be a good idea. else, for multiple actions on same page, multiactioncontrollers do the job real well.

            lemme know if u need more information on the same.