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

Java编程

开发平台:

Java

  1. package org.springframework.samples.petclinic.web;
  2. import java.util.Collection;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.samples.petclinic.Clinic;
  5. import org.springframework.samples.petclinic.Owner;
  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.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. /**
  12.  * JavaBean Form controller that is used to search for <code>Owner</code>s by
  13.  * last name.
  14.  *
  15.  * @author Juergen Hoeller
  16.  * @author Ken Krebs
  17.  */
  18. @Controller
  19. @RequestMapping("/findOwners.do")
  20. public class FindOwnersForm {
  21. private final Clinic clinic;
  22. @Autowired
  23. public FindOwnersForm(Clinic clinic) {
  24. this.clinic = clinic;
  25. }
  26. @RequestMapping(method = RequestMethod.GET)
  27. public  String setupForm(Model model) {
  28. model.addAttribute("owner", new Owner());
  29. return "findOwners";
  30. }
  31. @RequestMapping(method = RequestMethod.POST)
  32. public  String processSubmit(Owner owner, BindingResult result, Model model) {
  33. // find owners by last name
  34. Collection<Owner> results = this.clinic.findOwners(owner.getLastName());
  35. if (results.size() < 1) {
  36. // no owners found
  37. result.rejectValue("lastName", "notFound", "not found");
  38. return "findOwners";
  39. }
  40. if (results.size() > 1) {
  41. // multiple owners found
  42. model.addAttribute("selections", results);
  43. return "owners";
  44. }
  45. else {
  46. // 1 owner found
  47. owner = results.iterator().next();
  48. return "redirect:owner.do?ownerId=" + owner.getId();
  49. }
  50. }
  51. }