Announcement Announcement Module
Collapse
No announcement yet.
Repetitively Invoking onSubmit in SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Repetitively Invoking onSubmit in SimpleFormController

    I have a shopping cart page showing the products in a selected category. If the user submits the form without changing categories (e.g., via an "Empty Cart" button), I'd like to redisplay the data in the model; I don't need to go to the database for it.

    This works fine on the first submission of the form. However, the model is discarded after that, so all subsequent submissions run through formBackingObject again before onSubmit. (Surprisingly, the model given to onSubmit is the one the user sees, rather than what formBackingObject provides, so the user's changes aren't lost.) Of course, I behave as though this is the first view of the form and go to the database to re-retrieve what the model already contained.

    Is there anything I can put in onSubmit that would indicate that the model should be retained? I've tried returning TRUE from isFormSubmission on every POST, and errors.reject in onSubmit, but neither of these causes the model to be retained.

    Can this be done in some other sort of controller class?

    Thanks,
    Elly
    Last edited by eshevin; Oct 7th, 2008, 04:49 PM.

  • #2
    What you need is setRequireSession( true ); ...

    If you put setRequireSession( true ); in your controller constructor, then the formbacking object will be retained on the session, but it is removed from the session before onSubmit() is called;

    When you want to "re-invoke" the formView, you need to return from onSubmit() via return showForm(request, response, errors, dataMap );
    or the simpler case of return showForm(request, response, errors);
    instead of returning a ModelAndView, which would be normally be the
    successView().

    The first case allows adding extra data to the model if needed, but
    either showForm will "re-attach" the existing formbacking object to the session, and re-invoke referenceData( ...) to re-obtain the data-model.

    Assuming that your reference data is read-only and doesn't need to
    update in real time, you can add a private property to the controller
    and cache the built referenceData map and return it on the re-call
    of referenceData( ) without accessing the data source again.

    Reading the workflow description carefully in the JavaDoc for
    AbstractFormController and SimpleFormController is the 1st step.
    To grok the complete execution path, read the source code for
    ...web.servlet.AbstractFormController.java, and SimpleFormController.java.

    regards, john

    Comment


    • #3
      RE: What you need is setRequireSession( true ); ...

      If you put setRequireSession( true ); in your controller constructor, then the formbacking object will be retained on the session, but it is removed from the session before onSubmit() is called;
      I have sessionForm set to true in the servlet config file; I presume this accomplishes the same thing.

      The simpler form of showForm (without passing a map) did the trick, even when the contents of the form changes. I presume this is because, in keeping with the standard procedure, the command object I'm modifying is the one passed via the model in "errors," which is what will be displayed when this form of showForm is used.

      Thank you, John!

      Comment


      • #4
        Is there a reason you aren't using a validator? It seems like this would be the right tool to solve your problem. A validator would allow you to examine input after form submission, and reject if values are improperly entered, returning to the original form.

        The samples that come with spring have some good examples of validators in action. Check the OrderFormController class in jpetstore. It is an example of a validator that is used in an AbstractWizardFormController, but adapting it to a SimpleFormController is not difficult.

        edit: on second thought, I'm not sure I entirely understand what you're trying to do.

        Comment


        • #5
          Is there a reason you aren't using a validator?
          Yes. I've got almost nothing to validate. The user can enter an integer for the number of units he wants to purchase, but at the moment I'm using Javascript to validate it without necessitating a round trip.

          What I'm doing is part of a training excercise, and I am thinking of accommodating Javascript suppression; if I get around to that, a validator is the way to go.

          In the meantime, I want to let the user get to onSubmit in any of three ways: a "View Cart" button, an "Empty Cart" button, and a change in a dropdown list which lets the user view products in a different category. Only the first of these leads off this page.

          In the other two cases, I want to change the model contents before redisplaying: in the first, change any nonzero quantities to zero; in the second, change the collection of products the user sees. As I understand it, this won't work in a validator. I can fake an error to make Spring redisplay the screen, but it will be as it was before the user clicked: original quantities and category display, not the new values I want to see.

          Comment

          Working...
          X