diff --git a/pom.xml b/pom.xml index 874af64d2affb2eeabc35a1abff797c10eea5564..94162e6aaad17f41df0996cb6b5cb0d403382e82 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ <log4j.version>1.2.17</log4j.version> <hibernate.version>4.1.4.Final</hibernate.version> <aspectj.version>1.7.1</aspectj.version> + <hibernate.validator.version>4.2.0.Final</hibernate.validator.version> </properties> <dependencies> @@ -118,6 +119,12 @@ <version>${hibernate.version}</version> </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + <version>${hibernate.validator.version}</version> + </dependency> + <!-- ********************************************************************** ** SPRING DATA ** ********************************************************************** --> diff --git a/src/main/java/org/springframework/samples/petclinic/Owner.java b/src/main/java/org/springframework/samples/petclinic/Owner.java index 13639f9d2bd35d7df26aa815d10bec7080344d06..bde857ed25d9b2d4c12890a3bdee25a9c9a6e938 100644 --- a/src/main/java/org/springframework/samples/petclinic/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/Owner.java @@ -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") diff --git a/src/main/java/org/springframework/samples/petclinic/Person.java b/src/main/java/org/springframework/samples/petclinic/Person.java index 8bfa1b502ec03a5e14785dbb8e0af19067b592bf..06b03840ffca5e1228bb907ac583499b7bb35c1b 100644 --- a/src/main/java/org/springframework/samples/petclinic/Person.java +++ b/src/main/java/org/springframework/samples/petclinic/Person.java @@ -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() { diff --git a/src/main/java/org/springframework/samples/petclinic/validation/OwnerValidator.java b/src/main/java/org/springframework/samples/petclinic/validation/OwnerValidator.java deleted file mode 100644 index 04b6b7d58afd411579ef99b864db8e97e63e3921..0000000000000000000000000000000000000000 --- a/src/main/java/org/springframework/samples/petclinic/validation/OwnerValidator.java +++ /dev/null @@ -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; - } - } - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/AddOwnerController.java b/src/main/java/org/springframework/samples/petclinic/web/AddOwnerController.java deleted file mode 100644 index 21ee7097ba83d11e5b8bd4fd13e1bf5460cd9bdc..0000000000000000000000000000000000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/AddOwnerController.java +++ /dev/null @@ -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(); - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/EditOwnerController.java b/src/main/java/org/springframework/samples/petclinic/web/EditOwnerController.java deleted file mode 100644 index 8d396ee88eaae75bbfcd5c7ba2bc40602e27b691..0000000000000000000000000000000000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/EditOwnerController.java +++ /dev/null @@ -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(); - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/FindOwnersController.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java similarity index 50% rename from src/main/java/org/springframework/samples/petclinic/web/FindOwnersController.java rename to src/main/java/org/springframework/samples/petclinic/web/OwnerController.java index d0b0cefcfe161b21598699b795572074adb35ee3..7b9fb494bcf4377baae732e4fe5354f0a0ab7e57 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/FindOwnersController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java @@ -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(); + } + } } diff --git a/src/main/resources/spring/applicationContext-dao.xml b/src/main/resources/spring/applicationContext-dao.xml index 9cd0805ec7e4c06f0eedc9f105a58f7fd54cf846..1ce97187d5e76c7911d24397d8f2f69bb5291908 100644 --- a/src/main/resources/spring/applicationContext-dao.xml +++ b/src/main/resources/spring/applicationContext-dao.xml @@ -70,6 +70,7 @@ <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="${jpa.database}" p:showSql="${jpa.showSql}"/> </property> + <property name="persistenceUnitName" value="petclinic" /> <property name="packagesToScan"> <list> <value>org/springframework/samples/petclinic</value> diff --git a/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp b/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp index c8cee48acf2b5c16ea5c71b1a3c224eea933ed08..149090d1e80d08630e7ac75c67b965562ef49c94 100644 --- a/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp +++ b/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp @@ -45,9 +45,9 @@ </tr> </c:forEach> </table> + <jsp:include page="../footer.jsp"/> </div> - <jsp:include page="../footer.jsp"/> </body> </html> diff --git a/src/main/webapp/resources/html/tutorial.html b/src/main/webapp/resources/html/tutorial.html index f053269867fadbc4e8765a340287ecd78d2dc874..ada7140726c22499c65f51d6ef9e1b1e69196316 100644 --- a/src/main/webapp/resources/html/tutorial.html +++ b/src/main/webapp/resources/html/tutorial.html @@ -824,7 +824,7 @@ <strong>Owner</strong>s by last name. </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> to the system. </li> diff --git a/src/test/java/org/springframework/samples/petclinic/jpa/SpringDataClinicTests.java b/src/test/java/org/springframework/samples/petclinic/jpa/SpringDataClinicTests.java new file mode 100644 index 0000000000000000000000000000000000000000..a7e1768353cf5cea5a84afa934de5206fb2b2a7e --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/jpa/SpringDataClinicTests.java @@ -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 { + +} \ No newline at end of file