diff --git a/src/main/java/org/springframework/samples/petclinic/NamedEntity.java b/src/main/java/org/springframework/samples/petclinic/NamedEntity.java index f77348e241132134098394d8260879cbba741996..18c4714dc5578f063dcabbb453ba7dc04e8714a7 100644 --- a/src/main/java/org/springframework/samples/petclinic/NamedEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/NamedEntity.java @@ -3,6 +3,7 @@ package org.springframework.samples.petclinic; import javax.persistence.Column; import javax.persistence.MappedSuperclass; + /** * Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. * Used as a base class for objects needing these properties. diff --git a/src/main/java/org/springframework/samples/petclinic/Owner.java b/src/main/java/org/springframework/samples/petclinic/Owner.java index bde857ed25d9b2d4c12890a3bdee25a9c9a6e938..65e3df8fe80e7a12282bab080cb6a8843e5d3dce 100644 --- a/src/main/java/org/springframework/samples/petclinic/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/Owner.java @@ -12,8 +12,9 @@ import javax.persistence.Entity; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.validation.constraints.Digits; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; -import org.hibernate.validator.constraints.NotEmpty; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.core.style.ToStringCreator; @@ -24,19 +25,20 @@ import org.springframework.core.style.ToStringCreator; * @author Ken Krebs * @author Juergen Hoeller * @author Sam Brannen + * @author Michael Isvy */ @Entity @Table(name="owners") public class Owner extends Person { @Column(name="address") - @NotEmpty + @NotNull @Size(min = 1) private String address; - @Column(name="city") - @NotEmpty + @Column(name="city") + @NotNull @Size(min = 1) private String city; @Column(name="telephone") - @NotEmpty @Digits(fraction = 0, integer = 10) + @NotNull @Digits(fraction = 0, integer = 10) private String telephone; @OneToMany(cascade=CascadeType.ALL, mappedBy="owner") diff --git a/src/main/java/org/springframework/samples/petclinic/web/EditPetController.java b/src/main/java/org/springframework/samples/petclinic/web/EditPetController.java deleted file mode 100644 index 0a02fdaf2f726e0d7b8531bb89ef61081edf4f27..0000000000000000000000000000000000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/EditPetController.java +++ /dev/null @@ -1,80 +0,0 @@ - -package org.springframework.samples.petclinic.web; - -import java.util.Collection; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.Clinic; -import org.springframework.samples.petclinic.Pet; -import org.springframework.samples.petclinic.PetType; -import org.springframework.samples.petclinic.validation.PetValidator; -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 <code>Pet</code>. - * - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - */ -@Controller -@RequestMapping("/owners/*/pets/{petId}/edit") -@SessionAttributes("pet") -public class EditPetController { - - private final Clinic clinic; - - - @Autowired - public EditPetController(Clinic clinic) { - this.clinic = clinic; - } - - @ModelAttribute("types") - public Collection<PetType> populatePetTypes() { - return this.clinic.getPetTypes(); - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @RequestMapping(method = RequestMethod.GET) - public String setupForm(@PathVariable("petId") int petId, Model model) { - Pet pet = this.clinic.findPet(petId); - model.addAttribute("pet", pet); - return "pets/createOrUpdatePetForm"; - } - - @RequestMapping(method = { RequestMethod.PUT, RequestMethod.POST }) - public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { - new PetValidator().validate(pet, result); - if (result.hasErrors()) { - return "pets/createOrUpdatePetForm"; - } - else { - this.clinic.storePet(pet); - status.setComplete(); - return "redirect:/owners/" + pet.getOwner().getId(); - } - } - - @RequestMapping(method = RequestMethod.DELETE) - public String deletePet(@PathVariable int petId) { - Pet pet = this.clinic.findPet(petId); - this.clinic.deletePet(petId); - return "redirect:/owners/" + pet.getOwner().getId(); - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/AddPetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetController.java similarity index 56% rename from src/main/java/org/springframework/samples/petclinic/web/AddPetController.java rename to src/main/java/org/springframework/samples/petclinic/web/PetController.java index 99de31506f8f41d4becbd5fb65072b0e1dc2defb..b18ab995e2e8327bac67d2692d40236289070541 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/AddPetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -30,15 +30,14 @@ import org.springframework.web.bind.support.SessionStatus; * @author Arjen Poutsma */ @Controller -@RequestMapping("/owners/{ownerId}/pets/new") @SessionAttributes("pet") -public class AddPetController { +public class PetController { private final Clinic clinic; @Autowired - public AddPetController(Clinic clinic) { + public PetController(Clinic clinic) { this.clinic = clinic; } @@ -52,8 +51,8 @@ public class AddPetController { dataBinder.setDisallowedFields("id"); } - @RequestMapping(method = RequestMethod.GET) - public String setupForm(@PathVariable("ownerId") int ownerId, Model model) { + @RequestMapping( method = RequestMethod.GET) + public String initCreationForm(@PathVariable("ownerId") int ownerId, Model model) { Owner owner = this.clinic.findOwner(ownerId); Pet pet = new Pet(); owner.addPet(pet); @@ -61,8 +60,8 @@ public class AddPetController { return "pets/createOrUpdatePetForm"; } - @RequestMapping(method = RequestMethod.POST) - public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { + @RequestMapping(value="/owners/{ownerId}/pets/new", method = RequestMethod.POST) + public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { new PetValidator().validate(pet, result); if (result.hasErrors()) { return "pets/createOrUpdatePetForm"; @@ -73,5 +72,33 @@ public class AddPetController { return "redirect:/owners/" + pet.getOwner().getId(); } } + + @RequestMapping(value="/owners/*/pets/{petId}/edit", method = RequestMethod.GET) + public String initUpdateForm(@PathVariable("petId") int petId, Model model) { + Pet pet = this.clinic.findPet(petId); + model.addAttribute("pet", pet); + return "pets/createOrUpdatePetForm"; + } + + @RequestMapping(value="/owners/*/pets/{petId}/edit", method = { RequestMethod.PUT, RequestMethod.POST }) + public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { + // we're not using @Valid annotation here because it is easier to define such validation rule in Java + new PetValidator().validate(pet, result); + if (result.hasErrors()) { + return "pets/createOrUpdatePetForm"; + } + else { + this.clinic.storePet(pet); + status.setComplete(); + return "redirect:/owners/" + pet.getOwner().getId(); + } + } + + @RequestMapping(value="/owners/*/pets/{petId}/edit", method = RequestMethod.DELETE) + public String deletePet(@PathVariable("petId") int petId) { + Pet pet = this.clinic.findPet(petId); + this.clinic.deletePet(petId); + return "redirect:/owners/" + pet.getOwner().getId(); + } } diff --git a/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp b/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp index 205cfda8ab468f16b0e9ae829e749ffad28e26ab..6c0f8810efc7652c8dda711f85f5a970db6a23e0 100644 --- a/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp +++ b/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp @@ -36,13 +36,13 @@ <table class="table-buttons"> <tr> <td colspan="2" align="center"> - <spring:url value="{ownerId}/edit" var="editUrl"> + <spring:url value="{ownerId}/edit.html" var="editUrl"> <spring:param name="ownerId" value="${owner.id}" /> </spring:url> <a href="${fn:escapeXml(editUrl)}">Edit Owner</a> </td> <td> - <spring:url value="{ownerId}/pets/new" var="addUrl"> + <spring:url value="{ownerId}/pets/new.html" var="addUrl"> <spring:param name="ownerId" value="${owner.id}" /> </spring:url> <a href="${fn:escapeXml(addUrl)}">Add New Pet</a> diff --git a/src/main/webapp/resources/html/tutorial.html b/src/main/webapp/resources/html/tutorial.html index ada7140726c22499c65f51d6ef9e1b1e69196316..e1a408bc0753034621f1b1de4a5d8cfaba285bbf 100644 --- a/src/main/webapp/resources/html/tutorial.html +++ b/src/main/webapp/resources/html/tutorial.html @@ -834,7 +834,7 @@ A copy of the existing <strong>Owner</strong> is used for editing. </li> <li> - <span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.web.AddPetController</span> + <span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.web.PetController</span> is an annotation-driven, POJO <em>Form</em> controller that is used to add a new <strong>Pet</strong> to an existing <strong>Owner</strong>. </li>