From 3bcf84543ec586173731942a83178eed561f7d3a Mon Sep 17 00:00:00 2001 From: Antoine Rey <antoine.rey@free.fr> Date: Thu, 22 Oct 2015 08:36:29 +0200 Subject: [PATCH] #77 move Session scope attributes to the request scope instead --- .../petclinic/web/OwnerController.java | 9 +-- .../samples/petclinic/web/PetController.java | 57 +++++++++++-------- .../samples/petclinic/web/PetValidator.java | 2 - src/main/webapp/WEB-INF/jsp/exception.jsp | 1 + .../jsp/owners/createOrUpdateOwnerForm.jsp | 1 + .../webapp/WEB-INF/jsp/owners/findOwners.jsp | 1 + .../WEB-INF/jsp/owners/ownerDetails.jsp | 1 + .../webapp/WEB-INF/jsp/owners/ownersList.jsp | 1 + .../jsp/pets/createOrUpdatePetForm.jsp | 2 + .../jsp/pets/createOrUpdateVisitForm.jsp | 1 + src/main/webapp/WEB-INF/jsp/vets/vetList.jsp | 1 + src/main/webapp/WEB-INF/jsp/welcome.jsp | 1 + 12 files changed, 44 insertions(+), 34 deletions(-) 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 4b54e0f..c2d45bb 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 9fdae68..39c7f1c 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<String, Object> 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<String, Object> 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 3aefb02..d889d1a 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 3d7ef0b..3c7a5f4 100644 --- a/src/main/webapp/WEB-INF/jsp/exception.jsp +++ b/src/main/webapp/WEB-INF/jsp/exception.jsp @@ -1,5 +1,6 @@ <!DOCTYPE html> +<%@ 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 0086220..30329e1 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 @@ <!DOCTYPE html> +<%@ 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 662ac7f..f6b1930 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 @@ <!DOCTYPE html> +<%@ 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 e4a970a..8e7e10b 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 @@ <!DOCTYPE html> +<%@ 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 68e4b06..2e88f0f 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 @@ <!DOCTYPE html> +<%@ 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 387959b..985984a 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 @@ <!DOCTYPE html> +<%@ 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 @@ <form:form modelAttribute="pet" class="form-horizontal"> + <input type="hidden" name="id" value="${pet.id}"/> <div class="control-group" id="owner"> <label class="control-label">Owner </label> diff --git a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateVisitForm.jsp b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateVisitForm.jsp index a5eb87f..a90e757 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 @@ <!DOCTYPE html> +<%@ 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 d279471..1c57ea9 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 @@ <!DOCTYPE html> +<%@ 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 4bd4f76..a1cf5fc 100644 --- a/src/main/webapp/WEB-INF/jsp/welcome.jsp +++ b/src/main/webapp/WEB-INF/jsp/welcome.jsp @@ -1,5 +1,6 @@ <!DOCTYPE html> +<%@ page session="false" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> -- GitLab