EditOwnerForm.java
上传用户:dezhong
上传日期:2022-08-10
资源大小:167k
文件大小:2k
源码类别:

Java编程

开发平台:

Java

  1. package org.springframework.samples.petclinic.web;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.samples.petclinic.Clinic;
  4. import org.springframework.samples.petclinic.Owner;
  5. import org.springframework.samples.petclinic.validation.OwnerValidator;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.validation.BindingResult;
  9. import org.springframework.web.bind.annotation.ModelAttribute;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.SessionAttributes;
  14. import org.springframework.web.bind.support.SessionStatus;
  15. /**
  16.  * JavaBean Form controller that is used to edit an existing <code>Owner</code>.
  17.  *
  18.  * @author Juergen Hoeller
  19.  * @author Ken Krebs
  20.  */
  21. @Controller
  22. @RequestMapping("/editOwner.do")
  23. @SessionAttributes(types = Owner.class)
  24. public class EditOwnerForm {
  25. private final Clinic clinic;
  26. @Autowired
  27. public EditOwnerForm(Clinic clinic) {
  28. this.clinic = clinic;
  29. }
  30. @RequestMapping(method = RequestMethod.GET)
  31. public String setupForm(@RequestParam("ownerId") int ownerId, Model model) {
  32. Owner owner = this.clinic.loadOwner(ownerId);
  33. model.addAttribute(owner);
  34. return "ownerForm";
  35. }
  36. @RequestMapping(method = RequestMethod.POST)
  37. public String processSubmit(@ModelAttribute Owner owner, BindingResult result, SessionStatus status) {
  38. new OwnerValidator().validate(owner, result);
  39. if (result.hasErrors()) {
  40. return "ownerForm";
  41. }
  42. else {
  43. this.clinic.storeOwner(owner);
  44. status.setComplete();
  45. return "redirect:owner.do?ownerId=" + owner.getId();
  46. }
  47. }
  48. }