mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12:25:50 +00:00
#77 move Session scope attributes to the request scope instead
This commit is contained in:
parent
6a94d7e60a
commit
3bcf84543e
12 changed files with 44 additions and 34 deletions
|
@ -31,8 +31,6 @@ import org.springframework.web.bind.annotation.InitBinder;
|
|||
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;
|
||||
|
||||
/**
|
||||
|
@ -42,7 +40,6 @@ import org.springframework.web.servlet.ModelAndView;
|
|||
* @author Michael Isvy
|
||||
*/
|
||||
@Controller
|
||||
@SessionAttributes(types = Owner.class)
|
||||
public class OwnerController {
|
||||
|
||||
private final ClinicService clinicService;
|
||||
|
@ -66,12 +63,11 @@ public class OwnerController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/owners/new", method = RequestMethod.POST)
|
||||
public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
|
||||
public String processCreationForm(@Valid Owner owner, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
} else {
|
||||
this.clinicService.saveOwner(owner);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
@ -115,12 +111,11 @@ public class OwnerController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST)
|
||||
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
|
||||
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
} else {
|
||||
this.clinicService.saveOwner(owner);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,26 +15,20 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.samples.petclinic.model.PetType;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
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.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
|
@ -42,12 +36,11 @@ import javax.validation.Valid;
|
|||
* @author Arjen Poutsma
|
||||
*/
|
||||
@Controller
|
||||
@SessionAttributes("pet")
|
||||
@RequestMapping("/owners/{ownerId}")
|
||||
public class PetController {
|
||||
|
||||
private final ClinicService clinicService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public PetController(ClinicService clinicService) {
|
||||
this.clinicService = clinicService;
|
||||
|
@ -58,46 +51,60 @@ public class PetController {
|
|||
return this.clinicService.findPetTypes();
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
public void initBinder(WebDataBinder dataBinder) {
|
||||
@ModelAttribute("owner")
|
||||
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
|
||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||
return owner;
|
||||
}
|
||||
|
||||
@InitBinder("owner")
|
||||
public void initOwnerBinder(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@InitBinder("pet")
|
||||
public void initPetBinder(WebDataBinder dataBinder) {
|
||||
dataBinder.setValidator(new PetValidator());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET)
|
||||
public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) {
|
||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||
@RequestMapping(value = "/pets/new", method = RequestMethod.GET)
|
||||
public String initCreationForm(Owner owner, ModelMap model) {
|
||||
Pet pet = new Pet();
|
||||
owner.addPet(pet);
|
||||
model.put("pet", pet);
|
||||
return "pets/createOrUpdatePetForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST)
|
||||
public String processCreationForm(@Valid Pet pet, BindingResult result, SessionStatus status) {
|
||||
@RequestMapping(value = "/pets/new", method = RequestMethod.POST)
|
||||
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
|
||||
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null){
|
||||
result.rejectValue("name", "duplicate", "already exists");
|
||||
}
|
||||
if (result.hasErrors()) {
|
||||
model.put("pet", pet);
|
||||
return "pets/createOrUpdatePetForm";
|
||||
} else {
|
||||
owner.addPet(pet);
|
||||
this.clinicService.savePet(pet);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/*/pets/{petId}/edit", method = RequestMethod.GET)
|
||||
public String initUpdateForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||
@RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.GET)
|
||||
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
|
||||
Pet pet = this.clinicService.findPetById(petId);
|
||||
model.put("pet", pet);
|
||||
return "pets/createOrUpdatePetForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
|
||||
public String processUpdateForm(@Valid Pet pet, BindingResult result, SessionStatus status) {
|
||||
@RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.POST)
|
||||
public String processUpdateForm(@Valid Pet pet, Owner owner, BindingResult result, ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
model.put("pet", pet);
|
||||
return "pets/createOrUpdatePetForm";
|
||||
} else {
|
||||
owner.addPet(pet);
|
||||
this.clinicService.savePet(pet);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,8 +38,6 @@ public class PetValidator implements Validator {
|
|||
// name validation
|
||||
if (!StringUtils.hasLength(name)) {
|
||||
errors.rejectValue("name", "required", "required");
|
||||
} else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) {
|
||||
errors.rejectValue("name", "duplicate", "already exists");
|
||||
}
|
||||
|
||||
// type validation
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
||||
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
|
||||
|
@ -26,6 +27,7 @@
|
|||
|
||||
<form:form modelAttribute="pet"
|
||||
class="form-horizontal">
|
||||
<input type="hidden" name="id" value="${pet.id}"/>
|
||||
<div class="control-group" id="owner">
|
||||
<label class="control-label">Owner </label>
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<%@ page session="false" %>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
|
||||
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
||||
|
||||
|
|
Loading…
Reference in a new issue