Skip to content

Commit d381fb6

Browse files
author
Dave Syer
authored
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 spring-projects#946 and spring-projects#947
1 parent e870b18 commit d381fb6

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>org.springframework.boot</groupId>
1010
<artifactId>spring-boot-starter-parent</artifactId>
11-
<version>2.6.3</version>
11+
<version>2.6.6</version>
1212
</parent>
1313
<name>petclinic</name>
1414

src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.web.bind.WebDataBinder;
3030
import org.springframework.web.bind.annotation.GetMapping;
3131
import org.springframework.web.bind.annotation.InitBinder;
32+
import org.springframework.web.bind.annotation.ModelAttribute;
3233
import org.springframework.web.bind.annotation.PathVariable;
3334
import org.springframework.web.bind.annotation.PostMapping;
3435
import org.springframework.web.bind.annotation.RequestParam;
@@ -56,6 +57,11 @@ public void setAllowedFields(WebDataBinder dataBinder) {
5657
dataBinder.setDisallowedFields("id");
5758
}
5859

60+
@ModelAttribute("owner")
61+
public Owner findOwner(@PathVariable(name = "ownerId", required = false) Integer ownerId) {
62+
return ownerId == null ? new Owner() : this.owners.findById(ownerId);
63+
}
64+
5965
@GetMapping("/owners/new")
6066
public String initCreationForm(Map<String, Object> model) {
6167
Owner owner = new Owner();

src/main/java/org/springframework/samples/petclinic/owner/PetController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public Owner findOwner(@PathVariable("ownerId") int ownerId) {
5252
return this.owners.findById(ownerId);
5353
}
5454

55+
@ModelAttribute("pet")
56+
public Pet findPet(@PathVariable("ownerId") int ownerId,
57+
@PathVariable(name = "petId", required = false) Integer petId) {
58+
return petId == null ? new Pet() : this.owners.findById(ownerId).getPet(petId);
59+
}
60+
5561
@InitBinder("owner")
5662
public void initOwnerBinder(WebDataBinder dataBinder) {
5763
dataBinder.setDisallowedFields("id");

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ spring.thymeleaf.mode=HTML
88

99
# JPA
1010
spring.jpa.hibernate.ddl-auto=none
11-
spring.jpa.open-in-view=false
11+
spring.jpa.open-in-view=true
1212

1313
# Internationalization
1414
spring.messages.basename=messages/messages

src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,16 @@ void testProcessUpdateOwnerFormSuccess() throws Exception {
173173
.andExpect(view().name("redirect:/owners/{ownerId}"));
174174
}
175175

176+
@Test
177+
void testProcessUpdateOwnerFormUnchangedSuccess() throws Exception {
178+
mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)).andExpect(status().is3xxRedirection())
179+
.andExpect(view().name("redirect:/owners/{ownerId}"));
180+
}
181+
176182
@Test
177183
void testProcessUpdateOwnerFormHasErrors() throws Exception {
178184
mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe")
179-
.param("lastName", "Bloggs").param("city", "London")).andExpect(status().isOk())
185+
.param("lastName", "Bloggs").param("address", "").param("telephone", "")).andExpect(status().isOk())
180186
.andExpect(model().attributeHasErrors("owner"))
181187
.andExpect(model().attributeHasFieldErrors("owner", "address"))
182188
.andExpect(model().attributeHasFieldErrors("owner", "telephone"))

0 commit comments

Comments
 (0)