View Javadoc

    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package sample.contact;
    17
    18  import org.springframework.beans.factory.InitializingBean;
    19
    20  import org.springframework.util.Assert;
    21
    22  import org.springframework.web.bind.RequestUtils;
    23  import org.springframework.web.servlet.ModelAndView;
    24  import org.springframework.web.servlet.mvc.Controller;
    25
    26  import java.io.IOException;
    27
    28  import javax.servlet.ServletException;
    29  import javax.servlet.http.HttpServletRequest;
    30  import javax.servlet.http.HttpServletResponse;
    31
    32
    33  /***
    34   * Controller to delete a contact.
    35   *
    36   * @author Ben Alex
    37   * @version $Id: DeleteController.java,v 1.5 2005/11/04 04:15:57 benalex Exp $
    38   */
    39  public class DeleteController implements Controller, InitializingBean {
    40      //~ Instance fields ========================================================
    41
    42      private ContactManager contactManager;
    43
    44      //~ Methods ================================================================
    45
    46      public void setContactManager(ContactManager contact) {
    47          this.contactManager = contact;
    48      }
    49
    50      public ContactManager getContactManager() {
    51          return contactManager;
    52      }
    53
    54      public void afterPropertiesSet() throws Exception {
    55          Assert.notNull(contactManager,
    56              "A ContactManager implementation is required");
    57      }
    58
    59      public ModelAndView handleRequest(HttpServletRequest request,
    60          HttpServletResponse response) throws ServletException, IOException {
    61          int id = RequestUtils.getRequiredIntParameter(request, "contactId");
    62          Contact contact = contactManager.getById(new Long(id));
    63          contactManager.delete(contact);
    64
    65          return new ModelAndView("deleted", "contact", contact);
    66      }
    67  }