mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12:25:50 +00:00
Fix #89 Web layer: use @Valid whenever possible
This commit is contained in:
parent
a07cf69292
commit
80ff54ac03
2 changed files with 23 additions and 8 deletions
|
@ -34,6 +34,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
|
@ -57,8 +59,9 @@ public class PetController {
|
|||
}
|
||||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
public void initBinder(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
dataBinder.setValidator(new PetValidator());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET)
|
||||
|
@ -71,8 +74,7 @@ public class PetController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST)
|
||||
public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
|
||||
new PetValidator().validate(pet, result);
|
||||
public String processCreationForm(@Valid Pet pet, BindingResult result, SessionStatus status) {
|
||||
if (result.hasErrors()) {
|
||||
return "pets/createOrUpdatePetForm";
|
||||
} else {
|
||||
|
@ -90,9 +92,7 @@ public class PetController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST})
|
||||
public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
|
||||
// we're not using @Valid annotation here because it is easier to define such validation rule in Java
|
||||
new PetValidator().validate(pet, result);
|
||||
public String processUpdateForm(@Valid Pet pet, BindingResult result, SessionStatus status) {
|
||||
if (result.hasErrors()) {
|
||||
return "pets/createOrUpdatePetForm";
|
||||
} else {
|
||||
|
|
|
@ -18,16 +18,22 @@ package org.springframework.samples.petclinic.web;
|
|||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
/**
|
||||
* <code>Validator</code> for <code>Pet</code> forms.
|
||||
* <p>
|
||||
* We're not using Bean Validation annotations here because it is easier to define such validation rule in Java.
|
||||
* </p>
|
||||
*
|
||||
* @author Ken Krebs
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class PetValidator {
|
||||
public class PetValidator implements Validator {
|
||||
|
||||
public void validate(Pet pet, Errors errors) {
|
||||
@Override
|
||||
public void validate(Object obj, Errors errors) {
|
||||
Pet pet = (Pet) obj;
|
||||
String name = pet.getName();
|
||||
// name validation
|
||||
if (!StringUtils.hasLength(name)) {
|
||||
|
@ -47,4 +53,13 @@ public class PetValidator {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This Validator validates *just* Pet instances
|
||||
*/
|
||||
@Override
|
||||
public boolean supports(Class<?> clazz) {
|
||||
return Pet.class.equals(clazz);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue