Results 1 to 3 of 3

Thread: CommandController, when to use?

  1. #1
    Join Date
    Aug 2004
    Location
    San Francisco, CA
    Posts
    66

    Default 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.

    What scenarios have you found AbstractCommandController useful?

    Thanks in advance,
    Christian

  2. #2
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default

    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.
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  3. #3
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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


    Code:
    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

Similar Threads

  1. Replies: 4
    Last Post: Jul 20th, 2005, 04:45 AM
  2. Replies: 0
    Last Post: Jul 1st, 2005, 05:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •