PDA

View Full Version : CommandController, when to use?



cnelson
Aug 31st, 2004, 12:43 AM
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.

What scenarios have you found AbstractCommandController useful?

Thanks in advance,
Christian

kantorn
Sep 1st, 2004, 12:51 PM
Hi.

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.

Alef Arendsen
Sep 1st, 2004, 03:50 PM
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.

cheers,

Alef




public interface Emailer {
public void setEmailAddress(String email);
public String getEmailAddress();

public void setText(String);
public String getText();

public void email();
}

public class EmailController extends AbstractCommandController {

// zet command class op Emailer

protected ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object command) {
Email email = (Emailer)command;
email.email();
return new ModelAndView("success");
}

// overriden from AbstractCommandController
public Object getCommand(HttpServletRequest request) {
return getEmailer()

// returns emailer (configured using method injection)
public abstract Email getEmailer();
}

<bean name="/email.action" class="dummypackage.EmailController">
<lookup-method name="getEmailer" bean="emailer"/>
</bean>

<bean id="emailer" class="dummypackage.EmailerImpl" singleton="false">
<property name="mailSender">
<bean class="org.springframework.mail.javamail.JavaMailSender">
<property name="host">
<value>smtp.xs4all.nl</value>
</property>
</bean>
</property>
</bean>


p.s. package names and comments stripped, they included the name of a client