mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:35:49 +00:00
Moving "visit" object to request scope
Previously was in session scope (doesn’t scale as well)
This commit is contained in:
parent
92e7ab43be
commit
f0bf692767
1 changed files with 25 additions and 15 deletions
|
@ -27,12 +27,10 @@ import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.InitBinder;
|
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.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
@ -41,7 +39,6 @@ import org.springframework.web.servlet.ModelAndView;
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@SessionAttributes("visit")
|
|
||||||
public class VisitController {
|
public class VisitController {
|
||||||
|
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
@ -56,32 +53,45 @@ public class VisitController {
|
||||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||||
dataBinder.setDisallowedFields("id");
|
dataBinder.setDisallowedFields("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET)
|
/**
|
||||||
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
* Called before each and every @RequestMapping annotated method.
|
||||||
|
* 2 goals:
|
||||||
|
* - Make sure we always have fresh data
|
||||||
|
* - Since we do not use the session scope, make sure that Pet object always has an id
|
||||||
|
* (Even though id is not part of the form fields)
|
||||||
|
* @param petId
|
||||||
|
* @return Pet
|
||||||
|
*/
|
||||||
|
@ModelAttribute("visit")
|
||||||
|
public Visit loadPetWithVisit(@PathVariable("petId") int petId) {
|
||||||
Pet pet = this.clinicService.findPetById(petId);
|
Pet pet = this.clinicService.findPetById(petId);
|
||||||
Visit visit = new Visit();
|
Visit visit = new Visit();
|
||||||
pet.addVisit(visit);
|
pet.addVisit(visit);
|
||||||
model.put("visit", visit);
|
return visit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
||||||
|
@RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET)
|
||||||
|
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||||
return "pets/createOrUpdateVisitForm";
|
return "pets/createOrUpdateVisitForm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||||
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST)
|
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST)
|
||||||
public String processNewVisitForm(@Valid Visit visit, BindingResult result, SessionStatus status) {
|
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return "pets/createOrUpdateVisitForm";
|
return "pets/createOrUpdateVisitForm";
|
||||||
} else {
|
} else {
|
||||||
this.clinicService.saveVisit(visit);
|
this.clinicService.saveVisit(visit);
|
||||||
status.setComplete();
|
|
||||||
return "redirect:/owners/{ownerId}";
|
return "redirect:/owners/{ownerId}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/owners/*/pets/{petId}/visits", method = RequestMethod.GET)
|
@RequestMapping(value = "/owners/*/pets/{petId}/visits", method = RequestMethod.GET)
|
||||||
public ModelAndView showVisits(@PathVariable int petId) {
|
public String showVisits(@PathVariable int petId, Map<String, Object> model) {
|
||||||
ModelAndView mav = new ModelAndView("visitList");
|
model.put("visits", this.clinicService.findPetById(petId).getVisits());
|
||||||
mav.addObject("visits", this.clinicService.findPetById(petId).getVisits());
|
return "visitList";
|
||||||
return mav;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue