AddDeleteContactController.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2002-2016 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample.contact;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.validation.BindingResult;
  20. import org.springframework.validation.Validator;
  21. import org.springframework.web.bind.WebDataBinder;
  22. import org.springframework.web.bind.annotation.InitBinder;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RequestMethod;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.servlet.ModelAndView;
  27. /**
  28. *
  29. * @author Luke Taylor
  30. * @since 3.0
  31. */
  32. @Controller
  33. public class AddDeleteContactController {
  34. @Autowired
  35. private ContactManager contactManager;
  36. private final Validator validator = new WebContactValidator();
  37. /**
  38. * Displays the "add contact" form.
  39. */
  40. @RequestMapping(value = "/secure/add.htm", method = RequestMethod.GET)
  41. public ModelAndView addContactDisplay() {
  42. return new ModelAndView("add", "webContact", new WebContact());
  43. }
  44. @InitBinder
  45. public void initBinder(WebDataBinder binder) {
  46. System.out.println("A binder for object: " + binder.getObjectName());
  47. }
  48. /**
  49. * Handles the submission of the contact form, creating a new instance if the username
  50. * and email are valid.
  51. */
  52. @RequestMapping(value = "/secure/add.htm", method = RequestMethod.POST)
  53. public String addContact(WebContact form, BindingResult result) {
  54. validator.validate(form, result);
  55. if (result.hasErrors()) {
  56. return "add";
  57. }
  58. Contact contact = new Contact(form.getName(), form.getEmail());
  59. contactManager.create(contact);
  60. return "redirect:/secure/index.htm";
  61. }
  62. @RequestMapping(value = "/secure/del.htm", method = RequestMethod.GET)
  63. public ModelAndView delContact(@RequestParam("contactId") int contactId) {
  64. Contact contact = contactManager.getById(Long.valueOf(contactId));
  65. contactManager.delete(contact);
  66. return new ModelAndView("deleted", "contact", contact);
  67. }
  68. }