From 0fe479390b360bcb9e903b1ea749157dbbdde96a Mon Sep 17 00:00:00 2001 From: Mic Date: Wed, 16 Jan 2013 10:30:20 +0800 Subject: [PATCH] improvements to Owner Controller - migrated to bean validation annotations - merged to one single controller --- pom.xml | 7 ++ .../samples/petclinic/Owner.java | 5 ++ .../samples/petclinic/Person.java | 5 ++ .../petclinic/validation/OwnerValidator.java | 43 ------------ .../petclinic/web/AddOwnerController.java | 65 ------------------- .../petclinic/web/EditOwnerController.java | 65 ------------------- ...rsController.java => OwnerController.java} | 56 ++++++++++++++-- .../spring/applicationContext-dao.xml | 1 + .../webapp/WEB-INF/jsp/owners/ownersList.jsp | 2 +- src/main/webapp/resources/html/tutorial.html | 2 +- .../petclinic/jpa/SpringDataClinicTests.java | 19 ++++++ 11 files changed, 89 insertions(+), 181 deletions(-) delete mode 100644 src/main/java/org/springframework/samples/petclinic/validation/OwnerValidator.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/web/AddOwnerController.java delete mode 100644 src/main/java/org/springframework/samples/petclinic/web/EditOwnerController.java rename src/main/java/org/springframework/samples/petclinic/web/{FindOwnersController.java => OwnerController.java} (50%) create mode 100644 src/test/java/org/springframework/samples/petclinic/jpa/SpringDataClinicTests.java diff --git a/pom.xml b/pom.xml index 874af64d2..94162e6aa 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ 1.2.17 4.1.4.Final 1.7.1 + 4.2.0.Final @@ -118,6 +119,12 @@ ${hibernate.version} + + org.hibernate + hibernate-validator + ${hibernate.validator.version} + + diff --git a/src/main/java/org/springframework/samples/petclinic/Owner.java b/src/main/java/org/springframework/samples/petclinic/Owner.java index 13639f9d2..bde857ed2 100644 --- a/src/main/java/org/springframework/samples/petclinic/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/Owner.java @@ -11,7 +11,9 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.OneToMany; import javax.persistence.Table; +import javax.validation.constraints.Digits; +import org.hibernate.validator.constraints.NotEmpty; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.core.style.ToStringCreator; @@ -26,12 +28,15 @@ import org.springframework.core.style.ToStringCreator; @Entity @Table(name="owners") public class Owner extends Person { @Column(name="address") + @NotEmpty private String address; @Column(name="city") + @NotEmpty private String city; @Column(name="telephone") + @NotEmpty @Digits(fraction = 0, integer = 10) private String telephone; @OneToMany(cascade=CascadeType.ALL, mappedBy="owner") diff --git a/src/main/java/org/springframework/samples/petclinic/Person.java b/src/main/java/org/springframework/samples/petclinic/Person.java index 8bfa1b502..06b03840f 100644 --- a/src/main/java/org/springframework/samples/petclinic/Person.java +++ b/src/main/java/org/springframework/samples/petclinic/Person.java @@ -2,6 +2,9 @@ package org.springframework.samples.petclinic; import javax.persistence.Column; import javax.persistence.MappedSuperclass; +import javax.validation.constraints.NotNull; + +import org.hibernate.validator.constraints.NotEmpty; /** * Simple JavaBean domain object representing an person. @@ -12,9 +15,11 @@ import javax.persistence.MappedSuperclass; public class Person extends BaseEntity { @Column(name="first_name") + @NotEmpty protected String firstName; @Column(name="last_name") + @NotEmpty protected String lastName; public String getFirstName() { diff --git a/src/main/java/org/springframework/samples/petclinic/validation/OwnerValidator.java b/src/main/java/org/springframework/samples/petclinic/validation/OwnerValidator.java deleted file mode 100644 index 04b6b7d58..000000000 --- a/src/main/java/org/springframework/samples/petclinic/validation/OwnerValidator.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.springframework.samples.petclinic.validation; - -import org.springframework.samples.petclinic.Owner; -import org.springframework.util.StringUtils; -import org.springframework.validation.Errors; - -/** - * Validator for Owner forms. - * - * @author Ken Krebs - * @author Juergen Hoeller - */ -public class OwnerValidator { - - public void validate(Owner owner, Errors errors) { - if (!StringUtils.hasLength(owner.getFirstName())) { - errors.rejectValue("firstName", "required", "required"); - } - if (!StringUtils.hasLength(owner.getLastName())) { - errors.rejectValue("lastName", "required", "required"); - } - if (!StringUtils.hasLength(owner.getAddress())) { - errors.rejectValue("address", "required", "required"); - } - if (!StringUtils.hasLength(owner.getCity())) { - errors.rejectValue("city", "required", "required"); - } - - String telephone = owner.getTelephone(); - if (!StringUtils.hasLength(telephone)) { - errors.rejectValue("telephone", "required", "required"); - } - else { - for (int i = 0; i < telephone.length(); ++i) { - if ((Character.isDigit(telephone.charAt(i))) == false) { - errors.rejectValue("telephone", "nonNumeric", "non-numeric"); - break; - } - } - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/AddOwnerController.java b/src/main/java/org/springframework/samples/petclinic/web/AddOwnerController.java deleted file mode 100644 index 21ee7097b..000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/AddOwnerController.java +++ /dev/null @@ -1,65 +0,0 @@ - -package org.springframework.samples.petclinic.web; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.Clinic; -import org.springframework.samples.petclinic.Owner; -import org.springframework.samples.petclinic.validation.OwnerValidator; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.SessionAttributes; -import org.springframework.web.bind.support.SessionStatus; - -/** - * JavaBean form controller that is used to add a new Owner to the - * system. - * - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - */ -@Controller -@RequestMapping("/owners/new") -@SessionAttributes(types = Owner.class) -public class AddOwnerController { - - private final Clinic clinic; - - - @Autowired - public AddOwnerController(Clinic clinic) { - this.clinic = clinic; - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @RequestMapping(method = RequestMethod.GET) - public String setupForm(Model model) { - Owner owner = new Owner(); - model.addAttribute(owner); - return "owners/createOrUpdateOwnerForm"; - } - - @RequestMapping(method = RequestMethod.POST) - public String processSubmit(@ModelAttribute Owner owner, BindingResult result, SessionStatus status) { - new OwnerValidator().validate(owner, result); - if (result.hasErrors()) { - return "owners/createOrUpdateOwnerForm"; - } - else { - this.clinic.storeOwner(owner); - status.setComplete(); - return "redirect:/owners/" + owner.getId(); - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/EditOwnerController.java b/src/main/java/org/springframework/samples/petclinic/web/EditOwnerController.java deleted file mode 100644 index 8d396ee88..000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/EditOwnerController.java +++ /dev/null @@ -1,65 +0,0 @@ - -package org.springframework.samples.petclinic.web; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.Clinic; -import org.springframework.samples.petclinic.Owner; -import org.springframework.samples.petclinic.validation.OwnerValidator; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.SessionAttributes; -import org.springframework.web.bind.support.SessionStatus; - -/** - * JavaBean Form controller that is used to edit an existing Owner. - * - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - */ -@Controller -@RequestMapping("/owners/{ownerId}/edit") -@SessionAttributes(types = Owner.class) -public class EditOwnerController { - - private final Clinic clinic; - - - @Autowired - public EditOwnerController(Clinic clinic) { - this.clinic = clinic; - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @RequestMapping(method = RequestMethod.GET) - public String setupForm(@PathVariable("ownerId") int ownerId, Model model) { - Owner owner = this.clinic.findOwner(ownerId); - model.addAttribute(owner); - return "owners/createOrUpdateOwnerForm"; - } - - @RequestMapping(method = RequestMethod.PUT) - public String processSubmit(@ModelAttribute Owner owner, BindingResult result, SessionStatus status) { - new OwnerValidator().validate(owner, result); - if (result.hasErrors()) { - return "owners/createOrUpdateOwnerForm"; - } - else { - this.clinic.storeOwner(owner); - status.setComplete(); - return "redirect:/owners/" + owner.getId(); - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/FindOwnersController.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java similarity index 50% rename from src/main/java/org/springframework/samples/petclinic/web/FindOwnersController.java rename to src/main/java/org/springframework/samples/petclinic/web/OwnerController.java index d0b0cefcf..7b9fb494b 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/FindOwnersController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java @@ -3,6 +3,8 @@ package org.springframework.samples.petclinic.web; import java.util.Collection; +import javax.validation.Valid; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.Clinic; import org.springframework.samples.petclinic.Owner; @@ -11,25 +13,29 @@ import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.bind.support.SessionStatus; /** - * JavaBean Form controller that is used to search for Owners by - * last name. + * JavaBean form controller that is used to handle Owners . * * @author Juergen Hoeller * @author Ken Krebs * @author Arjen Poutsma + * @author Michael Isvy */ @Controller -public class FindOwnersController { +@SessionAttributes(types = Owner.class) +public class OwnerController { private final Clinic clinic; @Autowired - public FindOwnersController(Clinic clinic) { + public OwnerController(Clinic clinic) { this.clinic = clinic; } @@ -38,14 +44,33 @@ public class FindOwnersController { dataBinder.setDisallowedFields("id"); } + @RequestMapping(value="/owners/new", method = RequestMethod.GET) + public String initCreationForm(Model model) { + Owner owner = new Owner(); + model.addAttribute(owner); + return "owners/createOrUpdateOwnerForm"; + } + + @RequestMapping(value="/owners/new", method = RequestMethod.POST) + public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) { + if (result.hasErrors()) { + return "owners/createOrUpdateOwnerForm"; + } + else { + this.clinic.storeOwner(owner); + status.setComplete(); + return "redirect:/owners/" + owner.getId(); + } + } + @RequestMapping(value = "/owners/find", method = RequestMethod.GET) - public String setupForm(Model model) { + public String initFindForm(Model model) { model.addAttribute("owner", new Owner()); return "owners/findOwners"; } @RequestMapping(value = "/owners", method = RequestMethod.GET) - public String processSubmit(Owner owner, BindingResult result, Model model) { + public String processFindForm(Owner owner, BindingResult result, Model model) { // allow parameterless GET request for /owners to return all records if (owner.getLastName() == null) { @@ -70,5 +95,24 @@ public class FindOwnersController { return "redirect:/owners/" + owner.getId(); } } + + @RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.GET) + public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { + Owner owner = this.clinic.findOwner(ownerId); + model.addAttribute(owner); + return "owners/createOrUpdateOwnerForm"; + } + + @RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.PUT) + public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { + if (result.hasErrors()) { + return "owners/createOrUpdateOwnerForm"; + } + else { + this.clinic.storeOwner(owner); + status.setComplete(); + return "redirect:/owners/" + owner.getId(); + } + } } diff --git a/src/main/resources/spring/applicationContext-dao.xml b/src/main/resources/spring/applicationContext-dao.xml index 9cd0805ec..1ce97187d 100644 --- a/src/main/resources/spring/applicationContext-dao.xml +++ b/src/main/resources/spring/applicationContext-dao.xml @@ -70,6 +70,7 @@ + org/springframework/samples/petclinic diff --git a/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp b/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp index c8cee48ac..149090d1e 100644 --- a/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp +++ b/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp @@ -45,9 +45,9 @@ + - diff --git a/src/main/webapp/resources/html/tutorial.html b/src/main/webapp/resources/html/tutorial.html index f05326986..ada714072 100644 --- a/src/main/webapp/resources/html/tutorial.html +++ b/src/main/webapp/resources/html/tutorial.html @@ -824,7 +824,7 @@ Owners by last name.
  • - org.springframework.samples.petclinic.web.AddOwnerController + org.springframework.samples.petclinic.web.OwnerController is an annotation-driven, POJO Form controller that is used to add a new Owner to the system.
  • diff --git a/src/test/java/org/springframework/samples/petclinic/jpa/SpringDataClinicTests.java b/src/test/java/org/springframework/samples/petclinic/jpa/SpringDataClinicTests.java new file mode 100644 index 000000000..a7e176835 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/jpa/SpringDataClinicTests.java @@ -0,0 +1,19 @@ + +package org.springframework.samples.petclinic.jpa; + +import org.junit.runner.RunWith; +import org.springframework.samples.petclinic.AbstractClinicTests; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Michael Isvy + */ + +@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"}) +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles({"jpa","spring-data-jpa"}) +public class SpringDataClinicTests extends AbstractClinicTests { + +} \ No newline at end of file