mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12:25:50 +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.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.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
|
@ -41,7 +39,6 @@ import org.springframework.web.servlet.ModelAndView;
|
|||
* @author Michael Isvy
|
||||
*/
|
||||
@Controller
|
||||
@SessionAttributes("visit")
|
||||
public class VisitController {
|
||||
|
||||
private final ClinicService clinicService;
|
||||
|
@ -56,32 +53,45 @@ public class VisitController {
|
|||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
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);
|
||||
Visit visit = new Visit();
|
||||
pet.addVisit(visit);
|
||||
model.put("visit", visit);
|
||||
pet.addVisit(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";
|
||||
}
|
||||
|
||||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||
@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()) {
|
||||
return "pets/createOrUpdateVisitForm";
|
||||
} else {
|
||||
this.clinicService.saveVisit(visit);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/*/pets/{petId}/visits", method = RequestMethod.GET)
|
||||
public ModelAndView showVisits(@PathVariable int petId) {
|
||||
ModelAndView mav = new ModelAndView("visitList");
|
||||
mav.addObject("visits", this.clinicService.findPetById(petId).getVisits());
|
||||
return mav;
|
||||
public String showVisits(@PathVariable int petId, Map<String, Object> model) {
|
||||
model.put("visits", this.clinicService.findPetById(petId).getVisits());
|
||||
return "visitList";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue