[SPR-6174] DataBinder now uses var-args to set allowed/disallowed/required fields.
This commit is contained in:
parent
f08d15c86f
commit
0860f3bb8c
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -7,18 +8,18 @@ import org.springframework.samples.petclinic.validation.OwnerValidator;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
|
||||
/**
|
||||
* JavaBean form controller that is used to add a new <code>Owner</code> to
|
||||
* the system.
|
||||
*
|
||||
* JavaBean form controller that is used to add a new <code>Owner</code> to the
|
||||
* system.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -30,6 +31,7 @@ public class AddOwnerForm {
|
|||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public AddOwnerForm(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
|
|
@ -37,7 +39,7 @@ public class AddOwnerForm {
|
|||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields(new String[] {"id"});
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import java.util.Collection;
|
||||
|
|
@ -11,19 +12,19 @@ import org.springframework.samples.petclinic.validation.PetValidator;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
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.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
|
||||
/**
|
||||
* JavaBean form controller that is used to add a new <code>Pet</code> to the
|
||||
* system.
|
||||
*
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -35,6 +36,7 @@ public class AddPetForm {
|
|||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public AddPetForm(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
|
|
@ -47,7 +49,7 @@ public class AddPetForm {
|
|||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields(new String[] {"id"});
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -8,19 +9,19 @@ import org.springframework.samples.petclinic.validation.VisitValidator;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
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.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
|
||||
/**
|
||||
* JavaBean form controller that is used to add a new <code>Visit</code> to
|
||||
* the system.
|
||||
*
|
||||
* JavaBean form controller that is used to add a new <code>Visit</code> to the
|
||||
* system.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -32,6 +33,7 @@ public class AddVisitForm {
|
|||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public AddVisitForm(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
|
|
@ -39,7 +41,7 @@ public class AddVisitForm {
|
|||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields(new String[] {"id"});
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -7,18 +8,18 @@ import org.springframework.samples.petclinic.validation.OwnerValidator;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
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.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
|
||||
/**
|
||||
* JavaBean Form controller that is used to edit an existing <code>Owner</code>.
|
||||
*
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -30,6 +31,7 @@ public class EditOwnerForm {
|
|||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public EditOwnerForm(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
|
|
@ -37,7 +39,7 @@ public class EditOwnerForm {
|
|||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields(new String[] {"id"});
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import java.util.Collection;
|
||||
|
|
@ -10,18 +11,18 @@ import org.springframework.samples.petclinic.validation.PetValidator;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
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.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
|
||||
/**
|
||||
* JavaBean Form controller that is used to edit an existing <code>Pet</code>.
|
||||
*
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -33,6 +34,7 @@ public class EditPetForm {
|
|||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public EditPetForm(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
|
|
@ -45,7 +47,7 @@ public class EditPetForm {
|
|||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields(new String[] {"id"});
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
|
|
@ -55,7 +57,7 @@ public class EditPetForm {
|
|||
return "pets/form";
|
||||
}
|
||||
|
||||
@RequestMapping(method = {RequestMethod.PUT, RequestMethod.POST})
|
||||
@RequestMapping(method = { RequestMethod.PUT, RequestMethod.POST })
|
||||
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
|
||||
new PetValidator().validate(pet, result);
|
||||
if (result.hasErrors()) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import java.util.Collection;
|
||||
|
|
@ -8,15 +9,15 @@ import org.springframework.samples.petclinic.Owner;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
|
||||
/**
|
||||
* JavaBean Form controller that is used to search for <code>Owner</code>s by
|
||||
* last name.
|
||||
*
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -26,6 +27,7 @@ public class FindOwnersForm {
|
|||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public FindOwnersForm(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
|
|
@ -33,23 +35,23 @@ public class FindOwnersForm {
|
|||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields(new String[] {"id"});
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/search", method = RequestMethod.GET)
|
||||
public String setupForm(Model model) {
|
||||
public String setupForm(Model model) {
|
||||
model.addAttribute("owner", new Owner());
|
||||
return "owners/search";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners", method = RequestMethod.GET)
|
||||
public String processSubmit(Owner owner, BindingResult result, Model model) {
|
||||
|
||||
public String processSubmit(Owner owner, BindingResult result, Model model) {
|
||||
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
if(owner.getLastName() == null) {
|
||||
if (owner.getLastName() == null) {
|
||||
owner.setLastName(""); // empty string signifies broadest possible search
|
||||
}
|
||||
|
||||
|
||||
// find owners by last name
|
||||
Collection<Owner> results = this.clinic.findOwners(owner.getLastName());
|
||||
if (results.size() < 1) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue