Browse Source

[SPR-6174] DataBinder now uses var-args to set allowed/disallowed/required fields.

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2045 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Sam Brannen 17 years ago
parent
commit
2afe3e61d5
  1. 14
      org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddOwnerForm.java
  2. 12
      org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddPetForm.java
  3. 16
      org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddVisitForm.java
  4. 12
      org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/EditOwnerForm.java
  5. 14
      org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/EditPetForm.java
  6. 20
      org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/FindOwnersForm.java

14
org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddOwnerForm.java

@ -1,3 +1,4 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; 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.ModelAttribute;
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.annotation.SessionAttributes;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.support.SessionStatus; 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 * JavaBean form controller that is used to add a new <code>Owner</code> to the
* the system. * system.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
* @author Arjen Poutsma * @author Arjen Poutsma
@ -30,6 +31,7 @@ public class AddOwnerForm {
private final Clinic clinic; private final Clinic clinic;
@Autowired @Autowired
public AddOwnerForm(Clinic clinic) { public AddOwnerForm(Clinic clinic) {
this.clinic = clinic; this.clinic = clinic;
@ -37,7 +39,7 @@ public class AddOwnerForm {
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"}); dataBinder.setDisallowedFields("id");
} }
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)

12
org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddPetForm.java

@ -1,3 +1,4 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import java.util.Collection; import java.util.Collection;
@ -11,19 +12,19 @@ import org.springframework.samples.petclinic.validation.PetValidator;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; 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.ModelAttribute;
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.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.support.SessionStatus;
import org.springframework.web.bind.WebDataBinder;
/** /**
* JavaBean form controller that is used to add a new <code>Pet</code> to the * JavaBean form controller that is used to add a new <code>Pet</code> to the
* system. * system.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
* @author Arjen Poutsma * @author Arjen Poutsma
@ -35,6 +36,7 @@ public class AddPetForm {
private final Clinic clinic; private final Clinic clinic;
@Autowired @Autowired
public AddPetForm(Clinic clinic) { public AddPetForm(Clinic clinic) {
this.clinic = clinic; this.clinic = clinic;
@ -47,7 +49,7 @@ public class AddPetForm {
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"}); dataBinder.setDisallowedFields("id");
} }
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)

16
org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/AddVisitForm.java

@ -1,3 +1,4 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; 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.ModelAttribute;
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.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.support.SessionStatus;
import org.springframework.web.bind.WebDataBinder;
/** /**
* JavaBean form controller that is used to add a new <code>Visit</code> to * JavaBean form controller that is used to add a new <code>Visit</code> to the
* the system. * system.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
* @author Arjen Poutsma * @author Arjen Poutsma
@ -32,6 +33,7 @@ public class AddVisitForm {
private final Clinic clinic; private final Clinic clinic;
@Autowired @Autowired
public AddVisitForm(Clinic clinic) { public AddVisitForm(Clinic clinic) {
this.clinic = clinic; this.clinic = clinic;
@ -39,7 +41,7 @@ public class AddVisitForm {
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"}); dataBinder.setDisallowedFields("id");
} }
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)

12
org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/EditOwnerForm.java

@ -1,3 +1,4 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; 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.ModelAttribute;
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.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.support.SessionStatus;
import org.springframework.web.bind.WebDataBinder;
/** /**
* JavaBean Form controller that is used to edit an existing <code>Owner</code>. * JavaBean Form controller that is used to edit an existing <code>Owner</code>.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
* @author Arjen Poutsma * @author Arjen Poutsma
@ -30,6 +31,7 @@ public class EditOwnerForm {
private final Clinic clinic; private final Clinic clinic;
@Autowired @Autowired
public EditOwnerForm(Clinic clinic) { public EditOwnerForm(Clinic clinic) {
this.clinic = clinic; this.clinic = clinic;
@ -37,7 +39,7 @@ public class EditOwnerForm {
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"}); dataBinder.setDisallowedFields("id");
} }
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)

14
org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/EditPetForm.java

@ -1,3 +1,4 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import java.util.Collection; import java.util.Collection;
@ -10,18 +11,18 @@ import org.springframework.samples.petclinic.validation.PetValidator;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; 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.ModelAttribute;
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.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.support.SessionStatus;
import org.springframework.web.bind.WebDataBinder;
/** /**
* JavaBean Form controller that is used to edit an existing <code>Pet</code>. * JavaBean Form controller that is used to edit an existing <code>Pet</code>.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
* @author Arjen Poutsma * @author Arjen Poutsma
@ -33,6 +34,7 @@ public class EditPetForm {
private final Clinic clinic; private final Clinic clinic;
@Autowired @Autowired
public EditPetForm(Clinic clinic) { public EditPetForm(Clinic clinic) {
this.clinic = clinic; this.clinic = clinic;
@ -45,7 +47,7 @@ public class EditPetForm {
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"}); dataBinder.setDisallowedFields("id");
} }
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
@ -55,7 +57,7 @@ public class EditPetForm {
return "pets/form"; 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) { public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
new PetValidator().validate(pet, result); new PetValidator().validate(pet, result);
if (result.hasErrors()) { if (result.hasErrors()) {

20
org.springframework.samples.petclinic/src/main/java/org/springframework/samples/petclinic/web/FindOwnersForm.java

@ -1,3 +1,4 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import java.util.Collection; import java.util.Collection;
@ -8,15 +9,15 @@ import org.springframework.samples.petclinic.Owner;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; 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 * JavaBean Form controller that is used to search for <code>Owner</code>s by
* last name. * last name.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
* @author Arjen Poutsma * @author Arjen Poutsma
@ -26,6 +27,7 @@ public class FindOwnersForm {
private final Clinic clinic; private final Clinic clinic;
@Autowired @Autowired
public FindOwnersForm(Clinic clinic) { public FindOwnersForm(Clinic clinic) {
this.clinic = clinic; this.clinic = clinic;
@ -33,23 +35,23 @@ public class FindOwnersForm {
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"id"}); dataBinder.setDisallowedFields("id");
} }
@RequestMapping(value = "/owners/search", method = RequestMethod.GET) @RequestMapping(value = "/owners/search", method = RequestMethod.GET)
public String setupForm(Model model) { public String setupForm(Model model) {
model.addAttribute("owner", new Owner()); model.addAttribute("owner", new Owner());
return "owners/search"; return "owners/search";
} }
@RequestMapping(value = "/owners", method = RequestMethod.GET) @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 // 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 owner.setLastName(""); // empty string signifies broadest possible search
} }
// find owners by last name // find owners by last name
Collection<Owner> results = this.clinic.findOwners(owner.getLastName()); Collection<Owner> results = this.clinic.findOwners(owner.getLastName());
if (results.size() < 1) { if (results.size() < 1) {

Loading…
Cancel
Save