Announcement Announcement Module
No announcement yet.
redirecting a form due to its GET parameters Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • redirecting a form due to its GET parameters

    I have a form page that has links that go to the same form page; the link specifies what item the form operates on; e.g., on the form page for example I could have a link http://localhost/editGizmo.zug?itemId=2 and the form page it's on is viewGizmo.zug. The links go to the page they're on, with GET parameters.

    The links allow you to navigate to the previous and next items in a list of items to edit via the form. But instead of the "next" link having the actual itemId (for example, 3) I've decided (perhaps unwisely) to add an additional parameter, "prev" or "next", as in http://localhost/editGizmo.zug?itemId=2&next. So when you click on the link a request goes back to the form controller, and the referenceData() method uses the itemId and the presence of "next" to pull the next item from the database and stuff it in the Map, which referenceData returns, and the view displays it in the form for editing.

    The advantage of this method is that I don't have to go to the database to get the previous and next itemIds as part of building the page's referenceData for the initial form view; I only get them on demand when the user clicks on the links.

    But the disadvantage of this method is that when you use the prev and next links, the url in the address bar doesn't display the current itemId, but the one you came from, with the prev or next parameter; http://localhost/editGizmo.zug?itemId=2&next for example. So if you bookmark the page, it's referencing the "previous" item, not the current one.

    So what I'd like to do is when the controller gets one of these urls that has a next or prev parameter, have it figure out the correct itemId, and send the browser a redirect with that. For example, if they click on the link http://localhost/editGizmo.zug?itemId=2&next, the controller sends a redirect to http://localhost/editGizmo.zug?itemId=3.

    I'm using SimpleFormController but I can't figure out which method I need to use to do this.

    Or maybe I should have the links go to some intermediary controller which figures out the correct itemId and then it sends a redirect to the form page?

  • #2
    The simpler solution would be to rewrite your database query, so you get the previous, the current and the next item in one go.
    If that is a performance killer I would go for your solution. But on top of that I would place the item found into the session. That saves one database roundtrip, because formBackingObject() can fetch it from the session.



    • #3
      Ok, thanks.

      But for the sake of discussion, and largely because I want to understand how to do this, if I stick with my current method, is the way to handle the redirect part to add a showForm() method and in it do
      final Map<String, Object> map = referenceData(request, controlModel, errors);
      final ModelAndView mav = new ModelAndView(getFormView());
      return (mav);
      In other words, is this more or less the "default" implementation of showForm()? If so, then I'm thinking that prior to the above code I could do a redirect if necessary.


      • #4
        I just remembered; I tried this last night and the controlModel that showForm() is receiving is null. If I don't have a showForm() then my referenceData() is getting a command Object that is filled in with the GET parameters.


        • #5
          I'm also wondering if I can use one of getCommand, formBackingObject, or currentFormObject from within showForm() since its controlModel is null.

          I feel like all I need is a method that receives the GET parameters and returns a ModelAndView.

          I can't tell from the api documents if it's acceptable for a form controller to receive GET parameters that it can use for its initial display of the form.


          • #6
            I guess, that's why the request is in formBackingObject's argument list.
            Just make sure to document it somewhere.