From d381fb658cb435a04e2271ca85bd3e8627a5e7e4 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 11 Apr 2022 13:24:49 +0000 Subject: [PATCH] Use open session in view and populate model attrs Open session in view was switched off accidentally a while ago. Also the mapping changes recently meant that the changes to @Valid model attributes were not being propagated correctly. Fixes #946 and #947 --- pom.xml | 2 +- .../samples/petclinic/owner/OwnerController.java | 6 ++++++ .../samples/petclinic/owner/PetController.java | 6 ++++++ src/main/resources/application.properties | 2 +- .../samples/petclinic/owner/OwnerControllerTests.java | 8 +++++++- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index c7ee2e673..e1a374328 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.springframework.boot spring-boot-starter-parent - 2.6.3 + 2.6.6 petclinic diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index 92f43e0a9..5778ad93b 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -29,6 +29,7 @@ import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.GetMapping; 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.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -56,6 +57,11 @@ class OwnerController { dataBinder.setDisallowedFields("id"); } + @ModelAttribute("owner") + public Owner findOwner(@PathVariable(name = "ownerId", required = false) Integer ownerId) { + return ownerId == null ? new Owner() : this.owners.findById(ownerId); + } + @GetMapping("/owners/new") public String initCreationForm(Map model) { Owner owner = new Owner(); diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index 4ebc117b0..b511a9e55 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -52,6 +52,12 @@ class PetController { return this.owners.findById(ownerId); } + @ModelAttribute("pet") + public Pet findPet(@PathVariable("ownerId") int ownerId, + @PathVariable(name = "petId", required = false) Integer petId) { + return petId == null ? new Pet() : this.owners.findById(ownerId).getPet(petId); + } + @InitBinder("owner") public void initOwnerBinder(WebDataBinder dataBinder) { dataBinder.setDisallowedFields("id"); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4d4784e36..21b629ae8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,7 +8,7 @@ spring.thymeleaf.mode=HTML # JPA spring.jpa.hibernate.ddl-auto=none -spring.jpa.open-in-view=false +spring.jpa.open-in-view=true # Internationalization spring.messages.basename=messages/messages diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java index f93808e97..778a83e48 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -173,10 +173,16 @@ class OwnerControllerTests { .andExpect(view().name("redirect:/owners/{ownerId}")); } + @Test + void testProcessUpdateOwnerFormUnchangedSuccess() throws Exception { + mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)).andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/owners/{ownerId}")); + } + @Test void testProcessUpdateOwnerFormHasErrors() throws Exception { mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe") - .param("lastName", "Bloggs").param("city", "London")).andExpect(status().isOk()) + .param("lastName", "Bloggs").param("address", "").param("telephone", "")).andExpect(status().isOk()) .andExpect(model().attributeHasErrors("owner")) .andExpect(model().attributeHasFieldErrors("owner", "address")) .andExpect(model().attributeHasFieldErrors("owner", "telephone"))