mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:35:49 +00:00
Merged Pet Controllers into a single one
This commit is contained in:
parent
0fe479390b
commit
e9b6aff0d4
6 changed files with 45 additions and 95 deletions
|
@ -3,6 +3,7 @@ package org.springframework.samples.petclinic;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.MappedSuperclass;
|
import javax.persistence.MappedSuperclass;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>.
|
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>.
|
||||||
* Used as a base class for objects needing these properties.
|
* Used as a base class for objects needing these properties.
|
||||||
|
|
|
@ -12,8 +12,9 @@ import javax.persistence.Entity;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
import javax.validation.constraints.Digits;
|
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.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.core.style.ToStringCreator;
|
import org.springframework.core.style.ToStringCreator;
|
||||||
|
@ -24,19 +25,20 @@ import org.springframework.core.style.ToStringCreator;
|
||||||
* @author Ken Krebs
|
* @author Ken Krebs
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
@Entity @Table(name="owners")
|
@Entity @Table(name="owners")
|
||||||
public class Owner extends Person {
|
public class Owner extends Person {
|
||||||
@Column(name="address")
|
@Column(name="address")
|
||||||
@NotEmpty
|
@NotNull @Size(min = 1)
|
||||||
private String address;
|
private String address;
|
||||||
|
|
||||||
@Column(name="city")
|
@Column(name="city")
|
||||||
@NotEmpty
|
@NotNull @Size(min = 1)
|
||||||
private String city;
|
private String city;
|
||||||
|
|
||||||
@Column(name="telephone")
|
@Column(name="telephone")
|
||||||
@NotEmpty @Digits(fraction = 0, integer = 10)
|
@NotNull @Digits(fraction = 0, integer = 10)
|
||||||
private String telephone;
|
private String telephone;
|
||||||
|
|
||||||
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")
|
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -30,15 +30,14 @@ import org.springframework.web.bind.support.SessionStatus;
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/owners/{ownerId}/pets/new")
|
|
||||||
@SessionAttributes("pet")
|
@SessionAttributes("pet")
|
||||||
public class AddPetController {
|
public class PetController {
|
||||||
|
|
||||||
private final Clinic clinic;
|
private final Clinic clinic;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public AddPetController(Clinic clinic) {
|
public PetController(Clinic clinic) {
|
||||||
this.clinic = clinic;
|
this.clinic = clinic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,8 +51,8 @@ public class AddPetController {
|
||||||
dataBinder.setDisallowedFields("id");
|
dataBinder.setDisallowedFields("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET)
|
@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);
|
Owner owner = this.clinic.findOwner(ownerId);
|
||||||
Pet pet = new Pet();
|
Pet pet = new Pet();
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
|
@ -61,8 +60,29 @@ public class AddPetController {
|
||||||
return "pets/createOrUpdatePetForm";
|
return "pets/createOrUpdatePetForm";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.POST)
|
@RequestMapping(value="/owners/{ownerId}/pets/new", method = RequestMethod.POST)
|
||||||
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
|
public String processCreationForm(@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(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);
|
new PetValidator().validate(pet, result);
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return "pets/createOrUpdatePetForm";
|
return "pets/createOrUpdatePetForm";
|
||||||
|
@ -74,4 +94,11 @@ public class AddPetController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -36,13 +36,13 @@
|
||||||
<table class="table-buttons">
|
<table class="table-buttons">
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="2" align="center">
|
<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:param name="ownerId" value="${owner.id}" />
|
||||||
</spring:url>
|
</spring:url>
|
||||||
<a href="${fn:escapeXml(editUrl)}">Edit Owner</a>
|
<a href="${fn:escapeXml(editUrl)}">Edit Owner</a>
|
||||||
</td>
|
</td>
|
||||||
<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:param name="ownerId" value="${owner.id}" />
|
||||||
</spring:url>
|
</spring:url>
|
||||||
<a href="${fn:escapeXml(addUrl)}">Add New Pet</a>
|
<a href="${fn:escapeXml(addUrl)}">Add New Pet</a>
|
||||||
|
|
|
@ -834,7 +834,7 @@
|
||||||
A copy of the existing <strong>Owner</strong> is used for editing.
|
A copy of the existing <strong>Owner</strong> is used for editing.
|
||||||
</li>
|
</li>
|
||||||
<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>
|
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>.
|
to an existing <strong>Owner</strong>.
|
||||||
</li>
|
</li>
|
||||||
|
|
Loading…
Reference in a new issue