mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-14 13:51:12 +00:00
improvements to Owner Controller
- migrated to bean validation annotations - merged to one single controller
This commit is contained in:
parent
5432a1932c
commit
0fe479390b
11 changed files with 89 additions and 181 deletions
|
|
@ -11,7 +11,9 @@ import javax.persistence.Column;
|
|||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Digits;
|
||||
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import org.springframework.beans.support.MutableSortDefinition;
|
||||
import org.springframework.beans.support.PropertyComparator;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
|
@ -26,12 +28,15 @@ import org.springframework.core.style.ToStringCreator;
|
|||
@Entity @Table(name="owners")
|
||||
public class Owner extends Person {
|
||||
@Column(name="address")
|
||||
@NotEmpty
|
||||
private String address;
|
||||
|
||||
@Column(name="city")
|
||||
@NotEmpty
|
||||
private String city;
|
||||
|
||||
@Column(name="telephone")
|
||||
@NotEmpty @Digits(fraction = 0, integer = 10)
|
||||
private String telephone;
|
||||
|
||||
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@ package org.springframework.samples.petclinic;
|
|||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* Simple JavaBean domain object representing an person.
|
||||
|
|
@ -12,9 +15,11 @@ import javax.persistence.MappedSuperclass;
|
|||
public class Person extends BaseEntity {
|
||||
|
||||
@Column(name="first_name")
|
||||
@NotEmpty
|
||||
protected String firstName;
|
||||
|
||||
@Column(name="last_name")
|
||||
@NotEmpty
|
||||
protected String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
package org.springframework.samples.petclinic.validation;
|
||||
|
||||
import org.springframework.samples.petclinic.Owner;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.Errors;
|
||||
|
||||
/**
|
||||
* <code>Validator</code> for <code>Owner</code> forms.
|
||||
*
|
||||
* @author Ken Krebs
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class OwnerValidator {
|
||||
|
||||
public void validate(Owner owner, Errors errors) {
|
||||
if (!StringUtils.hasLength(owner.getFirstName())) {
|
||||
errors.rejectValue("firstName", "required", "required");
|
||||
}
|
||||
if (!StringUtils.hasLength(owner.getLastName())) {
|
||||
errors.rejectValue("lastName", "required", "required");
|
||||
}
|
||||
if (!StringUtils.hasLength(owner.getAddress())) {
|
||||
errors.rejectValue("address", "required", "required");
|
||||
}
|
||||
if (!StringUtils.hasLength(owner.getCity())) {
|
||||
errors.rejectValue("city", "required", "required");
|
||||
}
|
||||
|
||||
String telephone = owner.getTelephone();
|
||||
if (!StringUtils.hasLength(telephone)) {
|
||||
errors.rejectValue("telephone", "required", "required");
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < telephone.length(); ++i) {
|
||||
if ((Character.isDigit(telephone.charAt(i))) == false) {
|
||||
errors.rejectValue("telephone", "nonNumeric", "non-numeric");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.Owner;
|
||||
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.support.SessionStatus;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/owners/new")
|
||||
@SessionAttributes(types = Owner.class)
|
||||
public class AddOwnerController {
|
||||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public AddOwnerController(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public String setupForm(Model model) {
|
||||
Owner owner = new Owner();
|
||||
model.addAttribute(owner);
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
public String processSubmit(@ModelAttribute Owner owner, BindingResult result, SessionStatus status) {
|
||||
new OwnerValidator().validate(owner, result);
|
||||
if (result.hasErrors()) {
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
else {
|
||||
this.clinic.storeOwner(owner);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.Owner;
|
||||
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.support.SessionStatus;
|
||||
|
||||
/**
|
||||
* JavaBean Form controller that is used to edit an existing <code>Owner</code>.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/owners/{ownerId}/edit")
|
||||
@SessionAttributes(types = Owner.class)
|
||||
public class EditOwnerController {
|
||||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public EditOwnerController(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public String setupForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||
Owner owner = this.clinic.findOwner(ownerId);
|
||||
model.addAttribute(owner);
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.PUT)
|
||||
public String processSubmit(@ModelAttribute Owner owner, BindingResult result, SessionStatus status) {
|
||||
new OwnerValidator().validate(owner, result);
|
||||
if (result.hasErrors()) {
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
else {
|
||||
this.clinic.storeOwner(owner);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -3,6 +3,8 @@ package org.springframework.samples.petclinic.web;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.Owner;
|
||||
|
|
@ -11,25 +13,29 @@ 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.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;
|
||||
|
||||
/**
|
||||
* JavaBean Form controller that is used to search for <code>Owner</code>s by
|
||||
* last name.
|
||||
* JavaBean form controller that is used to handle <code>Owner</code>s .
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@Controller
|
||||
public class FindOwnersController {
|
||||
@SessionAttributes(types = Owner.class)
|
||||
public class OwnerController {
|
||||
|
||||
private final Clinic clinic;
|
||||
|
||||
|
||||
@Autowired
|
||||
public FindOwnersController(Clinic clinic) {
|
||||
public OwnerController(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
}
|
||||
|
||||
|
|
@ -38,14 +44,33 @@ public class FindOwnersController {
|
|||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@RequestMapping(value="/owners/new", method = RequestMethod.GET)
|
||||
public String initCreationForm(Model model) {
|
||||
Owner owner = new Owner();
|
||||
model.addAttribute(owner);
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value="/owners/new", method = RequestMethod.POST)
|
||||
public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
|
||||
if (result.hasErrors()) {
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
else {
|
||||
this.clinic.storeOwner(owner);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners/find", method = RequestMethod.GET)
|
||||
public String setupForm(Model model) {
|
||||
public String initFindForm(Model model) {
|
||||
model.addAttribute("owner", new Owner());
|
||||
return "owners/findOwners";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/owners", method = RequestMethod.GET)
|
||||
public String processSubmit(Owner owner, BindingResult result, Model model) {
|
||||
public String processFindForm(Owner owner, BindingResult result, Model model) {
|
||||
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
if (owner.getLastName() == null) {
|
||||
|
|
@ -70,5 +95,24 @@ public class FindOwnersController {
|
|||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.GET)
|
||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||
Owner owner = this.clinic.findOwner(ownerId);
|
||||
model.addAttribute(owner);
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.PUT)
|
||||
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
|
||||
if (result.hasErrors()) {
|
||||
return "owners/createOrUpdateOwnerForm";
|
||||
}
|
||||
else {
|
||||
this.clinic.storeOwner(owner);
|
||||
status.setComplete();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue