diff --git a/src/main/java/org/springframework/samples/petclinic/Pet.java b/src/main/java/org/springframework/samples/petclinic/Pet.java index 6235ba989..8974baeda 100644 --- a/src/main/java/org/springframework/samples/petclinic/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/Pet.java @@ -19,6 +19,7 @@ import org.hibernate.annotations.Type; import org.joda.time.DateTime; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; +import org.springframework.format.annotation.DateTimeFormat; /** * Simple JavaBean business object representing a pet. @@ -32,6 +33,7 @@ public class Pet extends NamedEntity { @Column(name="birth_date") @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") + @DateTimeFormat(pattern="yyyy/MM/dd") private DateTime birthDate; @ManyToOne diff --git a/src/main/java/org/springframework/samples/petclinic/web/ClinicBindingInitializer.java b/src/main/java/org/springframework/samples/petclinic/web/ClinicBindingInitializer.java deleted file mode 100644 index 0751d6c52..000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/ClinicBindingInitializer.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.springframework.samples.petclinic.web; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.propertyeditors.StringTrimmerEditor; -import org.springframework.samples.petclinic.PetType; -import org.springframework.samples.petclinic.service.ClinicService; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.support.WebBindingInitializer; -import org.springframework.web.context.request.WebRequest; - -/** - * Shared WebBindingInitializer for PetClinic's custom editors. - * - *

Alternatively, such init-binder code may be put into - * {@link org.springframework.web.bind.annotation.InitBinder} - * annotated methods on the controller classes themselves. - * - * @author Juergen Hoeller - */ -public class ClinicBindingInitializer implements WebBindingInitializer { - - @Autowired - private ClinicService clinicService; - - public void initBinder(WebDataBinder binder, WebRequest request) { - binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); - binder.registerCustomEditor(PetType.class, new PetTypeEditor(this.clinicService)); - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetTypeEditor.java b/src/main/java/org/springframework/samples/petclinic/web/PetTypeEditor.java deleted file mode 100644 index 801442576..000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/PetTypeEditor.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.springframework.samples.petclinic.web; - -import java.beans.PropertyEditorSupport; - -import org.springframework.samples.petclinic.PetType; -import org.springframework.samples.petclinic.service.ClinicService; - -/** - * @author Mark Fisher - * @author Juergen Hoeller - */ -public class PetTypeEditor extends PropertyEditorSupport { - - private final ClinicService clinicService; - - - public PetTypeEditor(ClinicService clinicService) { - this.clinicService = clinicService; - } - - @Override - public void setAsText(String text) throws IllegalArgumentException { - for (PetType type : this.clinicService.findPetTypes()) { - if (type.getName().equals(text)) { - setValue(type); - } - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java b/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java new file mode 100644 index 000000000..aad2e9d01 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java @@ -0,0 +1,44 @@ +package org.springframework.samples.petclinic.web; + + +import java.text.ParseException; +import java.util.Collection; +import java.util.Locale; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.format.Formatter; +import org.springframework.samples.petclinic.PetType; +import org.springframework.samples.petclinic.service.ClinicService; + +/** + * @author Mark Fisher + * @author Juergen Hoeller + * @author Michael Isvy + */ +public class PetTypeFormatter implements Formatter { + + private final ClinicService clinicService; + + + @Autowired + public PetTypeFormatter(ClinicService clinicService) { + this.clinicService = clinicService; + } + + @Override + public String print(PetType petType, Locale locale) { + return petType.getName(); + } + + @Override + public PetType parse(String text, Locale locale) throws ParseException { + Collection findPetTypes = this.clinicService.findPetTypes(); + for (PetType type : findPetTypes) { + if (type.getName().equals(text)) { + return type; + } + } + throw new ParseException("type not found: "+text, 0); + } + +} diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index 40506650e..c39d78d74 100755 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -11,21 +11,6 @@ - - - - - - - - - - - - - - - diff --git a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp index b118a6fc3..80042eec6 100644 --- a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp @@ -10,7 +10,7 @@

@@ -44,7 +44,7 @@
- +
diff --git a/src/main/webapp/WEB-INF/petclinic-servlet.xml b/src/main/webapp/WEB-INF/petclinic-servlet.xml index dbb255a2a..485b73d7e 100644 --- a/src/main/webapp/WEB-INF/petclinic-servlet.xml +++ b/src/main/webapp/WEB-INF/petclinic-servlet.xml @@ -16,7 +16,7 @@ - + @@ -28,6 +28,15 @@ + + + + + + + + + - - - - - - -