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 4b54e0f9b..c2d45bb8c 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java @@ -31,8 +31,6 @@ 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; import org.springframework.web.servlet.ModelAndView; /** @@ -42,7 +40,6 @@ import org.springframework.web.servlet.ModelAndView; * @author Michael Isvy */ @Controller -@SessionAttributes(types = Owner.class) public class OwnerController { private final ClinicService clinicService; @@ -66,12 +63,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(); } } @@ -115,12 +111,11 @@ public class OwnerController { } @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST) - public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { + public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result) { if (result.hasErrors()) { return "owners/createOrUpdateOwnerForm"; } else { 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 9fdae6855..39c7f1cb3 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -15,26 +15,20 @@ */ package org.springframework.samples.petclinic.web; -import java.util.Collection; -import java.util.Map; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.util.StringUtils; 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; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.Collection; /** * @author Juergen Hoeller @@ -42,12 +36,11 @@ import javax.validation.Valid; * @author Arjen Poutsma */ @Controller -@SessionAttributes("pet") +@RequestMapping("/owners/{ownerId}") public class PetController { private final ClinicService clinicService; - @Autowired public PetController(ClinicService clinicService) { this.clinicService = clinicService; @@ -58,46 +51,60 @@ public class PetController { return this.clinicService.findPetTypes(); } - @InitBinder - public void initBinder(WebDataBinder dataBinder) { + @ModelAttribute("owner") + public Owner findOwner(@PathVariable("ownerId") int ownerId) { + Owner owner = this.clinicService.findOwnerById(ownerId); + return owner; + } + + @InitBinder("owner") + public void initOwnerBinder(WebDataBinder dataBinder) { dataBinder.setDisallowedFields("id"); + } + + @InitBinder("pet") + public void initPetBinder(WebDataBinder dataBinder) { dataBinder.setValidator(new PetValidator()); } - @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET) - public String initCreationForm(@PathVariable("ownerId") int ownerId, Map model) { - Owner owner = this.clinicService.findOwnerById(ownerId); + @RequestMapping(value = "/pets/new", method = RequestMethod.GET) + public String initCreationForm(Owner owner, ModelMap model) { Pet pet = new Pet(); owner.addPet(pet); model.put("pet", pet); return "pets/createOrUpdatePetForm"; } - @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST) - public String processCreationForm(@Valid Pet pet, BindingResult result, SessionStatus status) { + @RequestMapping(value = "/pets/new", method = RequestMethod.POST) + public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) { + if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null){ + result.rejectValue("name", "duplicate", "already exists"); + } if (result.hasErrors()) { + model.put("pet", pet); return "pets/createOrUpdatePetForm"; } else { + owner.addPet(pet); this.clinicService.savePet(pet); - status.setComplete(); return "redirect:/owners/{ownerId}"; } } - @RequestMapping(value = "/owners/*/pets/{petId}/edit", method = RequestMethod.GET) - public String initUpdateForm(@PathVariable("petId") int petId, Map model) { + @RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.GET) + public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { Pet pet = this.clinicService.findPetById(petId); model.put("pet", pet); return "pets/createOrUpdatePetForm"; } - @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST) - public String processUpdateForm(@Valid Pet pet, BindingResult result, SessionStatus status) { + @RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.POST) + public String processUpdateForm(@Valid Pet pet, Owner owner, BindingResult result, ModelMap model) { if (result.hasErrors()) { + model.put("pet", pet); return "pets/createOrUpdatePetForm"; } else { + owner.addPet(pet); this.clinicService.savePet(pet); - status.setComplete(); return "redirect:/owners/{ownerId}"; } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java index 3aefb024e..d889d1af6 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java @@ -38,8 +38,6 @@ public class PetValidator implements Validator { // name validation if (!StringUtils.hasLength(name)) { errors.rejectValue("name", "required", "required"); - } else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) { - errors.rejectValue("name", "duplicate", "already exists"); } // type validation diff --git a/src/main/webapp/WEB-INF/jsp/exception.jsp b/src/main/webapp/WEB-INF/jsp/exception.jsp index 3d7ef0b01..3c7a5f404 100644 --- a/src/main/webapp/WEB-INF/jsp/exception.jsp +++ b/src/main/webapp/WEB-INF/jsp/exception.jsp @@ -1,5 +1,6 @@ +<%@ 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/owners/createOrUpdateOwnerForm.jsp b/src/main/webapp/WEB-INF/jsp/owners/createOrUpdateOwnerForm.jsp index 0086220ce..30329e107 100644 --- a/src/main/webapp/WEB-INF/jsp/owners/createOrUpdateOwnerForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/owners/createOrUpdateOwnerForm.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> diff --git a/src/main/webapp/WEB-INF/jsp/owners/findOwners.jsp b/src/main/webapp/WEB-INF/jsp/owners/findOwners.jsp index 662ac7ffd..f6b1930eb 100644 --- a/src/main/webapp/WEB-INF/jsp/owners/findOwners.jsp +++ b/src/main/webapp/WEB-INF/jsp/owners/findOwners.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> diff --git a/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp b/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp index e4a970a2c..8e7e10bbd 100644 --- a/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp +++ b/src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> diff --git a/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp b/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp index 68e4b0666..2e88f0fb8 100644 --- a/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp +++ b/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> diff --git a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp index 387959b5d..985984aba 100644 --- a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> @@ -26,6 +27,7 @@ +
diff --git a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateVisitForm.jsp b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateVisitForm.jsp index a5eb87fcd..a90e757f8 100644 --- a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateVisitForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateVisitForm.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> diff --git a/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp b/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp index d2794718d..1c57ea93c 100644 --- a/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp +++ b/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> diff --git a/src/main/webapp/WEB-INF/jsp/welcome.jsp b/src/main/webapp/WEB-INF/jsp/welcome.jsp index 4bd4f767f..a1cf5fcae 100644 --- a/src/main/webapp/WEB-INF/jsp/welcome.jsp +++ b/src/main/webapp/WEB-INF/jsp/welcome.jsp @@ -1,5 +1,6 @@ +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>