Announcement Announcement Module
No announcement yet.
Command classes and actions Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Command classes and actions

    I'm new to Spring MVC, and need some guidance around the design of command class(es) for the following simple scenario.

    I wish to create a data entry form for maintaining a small list of objects (customers). The form will be implemented as a JSP.

    The JSP will contain the following fields: a drop down list for customer selection purposes, 3 text boxes for keying the attributes of the currently selected (or new) customer and two buttons labelled "Save" and "Delete".

    The default entry in the drop down list will be "[Create New]". When the user initially navigates to the page this item will be selected and the 3 text boxes will be blank, ready for accepting the details of a new customer. The Delete button will be disabled. On keying the attributes for a new customer the user clicks Save, which submits the page, performs appropriate validation, saves the customer and refreshes the screen.

    If the user selects a different customer from the drop-down list the screen will refresh with the details of the selected customer, ready for amending or deletion.

    As can be seen, a number of actions can be invoked from the page:

    1. Initialse the screen for accepting a new customer
    2. Retrieve an existing customer
    3. Save a new/modified customer
    4. Delete the currently selected customer

    How is this type of scenario normally implemented in Spring MVC? I presume a subclass of SimpleFormController would be appropriate for the controller. I'm not sure about best practice for command classes however. Should I create a class called CustomerCommand (for example) with the following properties:

    action - a String to specify the action (Initialise, Retrieve, Save, Delete)
    customer - holds instance of Customer (the model class for customers)

    I'm slightly confused about this because the jpetstore demo app doesn't seem to include any command classes, and the Spring MVC step-by-step example only contains a trivial example.

    Any pointers would be much appreciated.



  • #2
    I probably wouldn't make a single form object that contains the action and Customer. Take a look at the Petclinic AddOwner and EditOwner samples.

    For your case I would have two URLs, one for add and one for edit. The initial load page would be add. Changing the select box would force a submission to the edit URL. The Cutomer id would be in this request.

    NB - look at isFormSubmission if you want the select submission to be a POST instead of a GET.