Merged Pet Controllers into a single one

This commit is contained in:
Mic 2013-01-16 10:57:50 +08:00
parent 0fe479390b
commit e9b6aff0d4
6 changed files with 45 additions and 95 deletions

View file

@ -3,6 +3,7 @@ package org.springframework.samples.petclinic;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
/**
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>.
* Used as a base class for objects needing these properties.

View file

@ -12,8 +12,9 @@ import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
@ -24,19 +25,20 @@ import org.springframework.core.style.ToStringCreator;
* @author Ken Krebs
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
@Entity @Table(name="owners")
public class Owner extends Person {
@Column(name="address")
@NotEmpty
@NotNull @Size(min = 1)
private String address;
@Column(name="city")
@NotEmpty
@NotNull @Size(min = 1)
private String city;
@Column(name="telephone")
@NotEmpty @Digits(fraction = 0, integer = 10)
@NotNull @Digits(fraction = 0, integer = 10)
private String telephone;
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")

View file

@ -1,80 +0,0 @@
package org.springframework.samples.petclinic.web;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
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.support.SessionStatus;
/**
* JavaBean Form controller that is used to edit an existing <code>Pet</code>.
*
* @author Juergen Hoeller
* @author Ken Krebs
* @author Arjen Poutsma
*/
@Controller
@RequestMapping("/owners/*/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetController {
private final Clinic clinic;
@Autowired
public EditPetController(Clinic clinic) {
this.clinic = clinic;
}
@ModelAttribute("types")
public Collection<PetType> populatePetTypes() {
return this.clinic.getPetTypes();
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@PathVariable("petId") int petId, Model model) {
Pet pet = this.clinic.findPet(petId);
model.addAttribute("pet", pet);
return "pets/createOrUpdatePetForm";
}
@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()) {
return "pets/createOrUpdatePetForm";
}
else {
this.clinic.storePet(pet);
status.setComplete();
return "redirect:/owners/" + pet.getOwner().getId();
}
}
@RequestMapping(method = RequestMethod.DELETE)
public String deletePet(@PathVariable int petId) {
Pet pet = this.clinic.findPet(petId);
this.clinic.deletePet(petId);
return "redirect:/owners/" + pet.getOwner().getId();
}
}

View file

@ -30,15 +30,14 @@ import org.springframework.web.bind.support.SessionStatus;
* @author Arjen Poutsma
*/
@Controller
@RequestMapping("/owners/{ownerId}/pets/new")
@SessionAttributes("pet")
public class AddPetController {
public class PetController {
private final Clinic clinic;
@Autowired
public AddPetController(Clinic clinic) {
public PetController(Clinic clinic) {
this.clinic = clinic;
}
@ -53,7 +52,7 @@ public class AddPetController {
}
@RequestMapping( method = RequestMethod.GET)
public String setupForm(@PathVariable("ownerId") int ownerId, Model model) {
public String initCreationForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.clinic.findOwner(ownerId);
Pet pet = new Pet();
owner.addPet(pet);
@ -61,8 +60,8 @@ public class AddPetController {
return "pets/createOrUpdatePetForm";
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
@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);
if (result.hasErrors()) {
return "pets/createOrUpdatePetForm";
@ -74,4 +73,32 @@ public class AddPetController {
}
}
@RequestMapping(value="/owners/*/pets/{petId}/edit", method = RequestMethod.GET)
public String initUpdateForm(@PathVariable("petId") int petId, Model model) {
Pet pet = this.clinic.findPet(petId);
model.addAttribute("pet", pet);
return "pets/createOrUpdatePetForm";
}
@RequestMapping(value="/owners/*/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);
if (result.hasErrors()) {
return "pets/createOrUpdatePetForm";
}
else {
this.clinic.storePet(pet);
status.setComplete();
return "redirect:/owners/" + pet.getOwner().getId();
}
}
@RequestMapping(value="/owners/*/pets/{petId}/edit", method = RequestMethod.DELETE)
public String deletePet(@PathVariable("petId") int petId) {
Pet pet = this.clinic.findPet(petId);
this.clinic.deletePet(petId);
return "redirect:/owners/" + pet.getOwner().getId();
}
}

View file

@ -36,13 +36,13 @@
<table class="table-buttons">
<tr>
<td colspan="2" align="center">
<spring:url value="{ownerId}/edit" var="editUrl">
<spring:url value="{ownerId}/edit.html" var="editUrl">
<spring:param name="ownerId" value="${owner.id}" />
</spring:url>
<a href="${fn:escapeXml(editUrl)}">Edit Owner</a>
</td>
<td>
<spring:url value="{ownerId}/pets/new" var="addUrl">
<spring:url value="{ownerId}/pets/new.html" var="addUrl">
<spring:param name="ownerId" value="${owner.id}" />
</spring:url>
<a href="${fn:escapeXml(addUrl)}">Add New Pet</a>

View file

@ -834,7 +834,7 @@
A copy of the existing <strong>Owner</strong> is used for editing.
</li>
<li>
<span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.web.AddPetController</span>
<span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.web.PetController</span>
is an annotation-driven, POJO <em>Form</em> controller that is used to add a new <strong>Pet</strong>
to an existing <strong>Owner</strong>.
</li>