mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-16 04:35:49 +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
7
pom.xml
7
pom.xml
|
@ -13,6 +13,7 @@
|
||||||
<log4j.version>1.2.17</log4j.version>
|
<log4j.version>1.2.17</log4j.version>
|
||||||
<hibernate.version>4.1.4.Final</hibernate.version>
|
<hibernate.version>4.1.4.Final</hibernate.version>
|
||||||
<aspectj.version>1.7.1</aspectj.version>
|
<aspectj.version>1.7.1</aspectj.version>
|
||||||
|
<hibernate.validator.version>4.2.0.Final</hibernate.validator.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
@ -118,6 +119,12 @@
|
||||||
<version>${hibernate.version}</version>
|
<version>${hibernate.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>${hibernate.validator.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- **********************************************************************
|
<!-- **********************************************************************
|
||||||
** SPRING DATA **
|
** SPRING DATA **
|
||||||
********************************************************************** -->
|
********************************************************************** -->
|
||||||
|
|
|
@ -11,7 +11,9 @@ import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
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 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;
|
||||||
|
@ -26,12 +28,15 @@ import org.springframework.core.style.ToStringCreator;
|
||||||
@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
|
||||||
private String address;
|
private String address;
|
||||||
|
|
||||||
@Column(name="city")
|
@Column(name="city")
|
||||||
|
@NotEmpty
|
||||||
private String city;
|
private String city;
|
||||||
|
|
||||||
@Column(name="telephone")
|
@Column(name="telephone")
|
||||||
|
@NotEmpty @Digits(fraction = 0, integer = 10)
|
||||||
private String telephone;
|
private String telephone;
|
||||||
|
|
||||||
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")
|
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")
|
||||||
|
|
|
@ -2,6 +2,9 @@ package org.springframework.samples.petclinic;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.MappedSuperclass;
|
import javax.persistence.MappedSuperclass;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
import org.hibernate.validator.constraints.NotEmpty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing an person.
|
* Simple JavaBean domain object representing an person.
|
||||||
|
@ -12,9 +15,11 @@ import javax.persistence.MappedSuperclass;
|
||||||
public class Person extends BaseEntity {
|
public class Person extends BaseEntity {
|
||||||
|
|
||||||
@Column(name="first_name")
|
@Column(name="first_name")
|
||||||
|
@NotEmpty
|
||||||
protected String firstName;
|
protected String firstName;
|
||||||
|
|
||||||
@Column(name="last_name")
|
@Column(name="last_name")
|
||||||
|
@NotEmpty
|
||||||
protected String lastName;
|
protected String lastName;
|
||||||
|
|
||||||
public String getFirstName() {
|
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 java.util.Collection;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.samples.petclinic.Clinic;
|
import org.springframework.samples.petclinic.Clinic;
|
||||||
import org.springframework.samples.petclinic.Owner;
|
import org.springframework.samples.petclinic.Owner;
|
||||||
|
@ -11,25 +13,29 @@ 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.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.InitBinder;
|
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.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.support.SessionStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JavaBean Form controller that is used to search for <code>Owner</code>s by
|
* JavaBean form controller that is used to handle <code>Owner</code>s .
|
||||||
* last name.
|
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Ken Krebs
|
* @author Ken Krebs
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @author Michael Isvy
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
public class FindOwnersController {
|
@SessionAttributes(types = Owner.class)
|
||||||
|
public class OwnerController {
|
||||||
|
|
||||||
private final Clinic clinic;
|
private final Clinic clinic;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public FindOwnersController(Clinic clinic) {
|
public OwnerController(Clinic clinic) {
|
||||||
this.clinic = clinic;
|
this.clinic = clinic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,14 +44,33 @@ public class FindOwnersController {
|
||||||
dataBinder.setDisallowedFields("id");
|
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)
|
@RequestMapping(value = "/owners/find", method = RequestMethod.GET)
|
||||||
public String setupForm(Model model) {
|
public String initFindForm(Model model) {
|
||||||
model.addAttribute("owner", new Owner());
|
model.addAttribute("owner", new Owner());
|
||||||
return "owners/findOwners";
|
return "owners/findOwners";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/owners", method = RequestMethod.GET)
|
@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
|
// allow parameterless GET request for /owners to return all records
|
||||||
if (owner.getLastName() == null) {
|
if (owner.getLastName() == null) {
|
||||||
|
@ -70,5 +95,24 @@ public class FindOwnersController {
|
||||||
return "redirect:/owners/" + owner.getId();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -70,6 +70,7 @@
|
||||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
|
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
|
||||||
p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
|
p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="persistenceUnitName" value="petclinic" />
|
||||||
<property name="packagesToScan">
|
<property name="packagesToScan">
|
||||||
<list>
|
<list>
|
||||||
<value>org/springframework/samples/petclinic</value>
|
<value>org/springframework/samples/petclinic</value>
|
||||||
|
|
|
@ -45,9 +45,9 @@
|
||||||
</tr>
|
</tr>
|
||||||
</c:forEach>
|
</c:forEach>
|
||||||
</table>
|
</table>
|
||||||
|
<jsp:include page="../footer.jsp"/>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<jsp:include page="../footer.jsp"/>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -824,7 +824,7 @@
|
||||||
<strong>Owner</strong>s by last name.
|
<strong>Owner</strong>s by last name.
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.web.AddOwnerController</span>
|
<span style="font-weight: bold; font-style: italic;">org.springframework.samples.petclinic.web.OwnerController</span>
|
||||||
is an annotation-driven, POJO <em>Form</em> controller that is used to add a new <strong>Owner</strong>
|
is an annotation-driven, POJO <em>Form</em> controller that is used to add a new <strong>Owner</strong>
|
||||||
to the system.
|
to the system.
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
package org.springframework.samples.petclinic.jpa;
|
||||||
|
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Michael Isvy
|
||||||
|
*/
|
||||||
|
|
||||||
|
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ActiveProfiles({"jpa","spring-data-jpa"})
|
||||||
|
public class SpringDataClinicTests extends AbstractClinicTests {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue