diff --git a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java index 8dfbfe90b..04bcf3ea3 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java @@ -45,4 +45,20 @@ public class BaseEntity { return (this.id == null); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + BaseEntity that = (BaseEntity) o; + + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java index b05ecbf86..322c972fc 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java @@ -23,7 +23,6 @@ import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; -import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; @@ -36,7 +35,6 @@ import java.util.Collection; * @author Michael Isvy */ @Controller -@SessionAttributes(types = Owner.class) public class OwnerController { private final ClinicService clinicService; @@ -60,12 +58,11 @@ public class OwnerController { } @RequestMapping(value = "/owners/new", method = RequestMethod.POST) - public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) { + public String processCreationForm(@Valid Owner owner, BindingResult result) { if (result.hasErrors()) { return "owners/createOrUpdateOwnerForm"; } else { this.clinicService.saveOwner(owner); - status.setComplete(); return "redirect:/owners/" + owner.getId(); } } @@ -110,12 +107,12 @@ public class OwnerController { } @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.PUT) - public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { + public String processUpdateOwnerForm(@PathVariable("ownerId") int ownerId, @Valid Owner owner, BindingResult result) { if (result.hasErrors()) { return "owners/createOrUpdateOwnerForm"; } else { + owner.setId(ownerId); this.clinicService.saveOwner(owner); - status.setComplete(); return "redirect:/owners/{ownerId}"; } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetController.java index 3161071d9..44f73acf7 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -25,7 +25,6 @@ import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; -import org.springframework.web.bind.support.SessionStatus; import java.util.Collection; @@ -35,7 +34,6 @@ import java.util.Collection; * @author Arjen Poutsma */ @Controller -@SessionAttributes("pet") public class PetController { private final ClinicService clinicService; @@ -66,13 +64,14 @@ public class PetController { } @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST) - public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { + public String processCreationForm(@PathVariable("ownerId") int ownerId, @ModelAttribute("pet") Pet pet, BindingResult result) { + Owner owner = this.clinicService.findOwnerById(ownerId); + owner.addPet(pet); new PetValidator().validate(pet, result); if (result.hasErrors()) { return "pets/createOrUpdatePetForm"; } else { this.clinicService.savePet(pet); - status.setComplete(); return "redirect:/owners/{ownerId}"; } } @@ -85,14 +84,20 @@ public class PetController { } @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST}) - public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { + public String processUpdateForm(@PathVariable("ownerId") int ownerId, + @PathVariable("petId") int petId, + @ModelAttribute("pet") Pet pet, + BindingResult result) { + + pet.setId(petId); + Owner owner = this.clinicService.findOwnerById(ownerId); + owner.addPet(pet); // 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.clinicService.savePet(pet); - status.setComplete(); return "redirect:/owners/{ownerId}"; } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java b/src/main/java/org/springframework/samples/petclinic/web/VisitController.java index 7de966939..d2d6d9448 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VisitController.java @@ -24,7 +24,6 @@ import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; -import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; @@ -36,7 +35,6 @@ import javax.validation.Valid; * @author Michael Isvy */ @Controller -@SessionAttributes("visit") public class VisitController { private final ClinicService clinicService; @@ -62,12 +60,13 @@ public class VisitController { } @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST) - public String processNewVisitForm(@Valid Visit visit, BindingResult result, SessionStatus status) { + public String processNewVisitForm(@PathVariable("petId") int petId, @Valid Visit visit, BindingResult result) { + Pet pet = this.clinicService.findPetById(petId); + visit.setPet(pet); if (result.hasErrors()) { return "pets/createOrUpdateVisitForm"; } else { - this.clinicService.saveVisit(visit); - status.setComplete(); + this.clinicService.saveVisit(visit);; return "redirect:/owners/{ownerId}"; } } diff --git a/src/main/webapp/WEB-INF/jsp/exception.jsp b/src/main/webapp/WEB-INF/jsp/exception.jsp old mode 100644 new mode 100755 index 86416dbaa..9f3765208 --- a/src/main/webapp/WEB-INF/jsp/exception.jsp +++ b/src/main/webapp/WEB-INF/jsp/exception.jsp @@ -1,3 +1,4 @@ +<%@page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> diff --git a/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp b/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp old mode 100644 new mode 100755 index 76c184417..6c0d8194a --- a/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp +++ b/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp @@ -1,3 +1,4 @@ +<%@page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> diff --git a/src/main/webapp/WEB-INF/jsp/fragments/footer.jsp b/src/main/webapp/WEB-INF/jsp/fragments/footer.jsp old mode 100644 new mode 100755 index dccec75d2..8b67308e7 --- a/src/main/webapp/WEB-INF/jsp/fragments/footer.jsp +++ b/src/main/webapp/WEB-INF/jsp/fragments/footer.jsp @@ -1,3 +1,4 @@ +<%@page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>