This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

CommandController, when to use?

I have a pretty basic question. I'm using spring's MVC, and loving it. I've made much use of the SimpleFormController, as well as my own implementation of the top level Controller interface.

When would one normally use the AbstractCommandController? I haven't needed it yet, and don't recall there being any examples in the distribution which use it.

What about this common scenario:

1) A page dynamically displays zero of more widgets.
2) User is able to select any one widget.
3) User presses the "edit widget" button.
4) The widget editor (a SimpleFormController) displays the selected widget in a form and allows changes to be made, and the form is validated before the changes are applied.

Would you subclass AbstractCommandController for the "selection page" above (1-3)? Or would you just use the SimpleFormController again? I personally have been using my own implementations of Controller for that type of scenario.

I suggest you browse the sources for /samples/petclinic in the Spring distribution folder, it's pretty instructive. You can copy, rename etc the petclinic controllers and adapt to the needs you have.

Quote Spring API docs from AbstractCommandController

Abstract base class for custom command controllers. Autopopulates a command bean from the request. For command validation, a validator (property inherited from BaseCommandController) can be used.

This command controller should preferrable not be used to handle form submission, because functionality for forms is more offered in more detail by the AbstractFormController and its corresponding implementations.

Seems like your needs involves a form to me. Use SimpleFormController.

Comment

Have a look at the following code that takes care of sending an email. It contains both emailing functionality as well as the data required to be able to email (email address, text). Something a colleague of mine wrote a couple of weeks ago.