Skip to content

Commit

Permalink
Use open session in view and populate model attrs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dsyer authored Apr 11, 2022
1 parent e870b18 commit d381fb6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<version>2.6.6</version>
</parent>
<name>petclinic</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -56,6 +57,11 @@ public void setAllowedFields(WebDataBinder dataBinder) {
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<String, Object> model) {
Owner owner = new Owner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public Owner findOwner(@PathVariable("ownerId") int ownerId) {
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");
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,16 @@ void testProcessUpdateOwnerFormSuccess() throws Exception {
.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"))
Expand Down

0 comments on commit d381fb6

Please sign in to comment.