fixed issues in the Pet creation form

This commit is contained in:
Mic 2013-02-06 17:33:11 +08:00
parent 6eae69719f
commit 57c9043121
7 changed files with 58 additions and 87 deletions

View file

@ -19,6 +19,7 @@ import org.hibernate.annotations.Type;
import org.joda.time.DateTime; import org.joda.time.DateTime;
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.format.annotation.DateTimeFormat;
/** /**
* Simple JavaBean business object representing a pet. * Simple JavaBean business object representing a pet.
@ -32,6 +33,7 @@ public class Pet extends NamedEntity {
@Column(name="birth_date") @Column(name="birth_date")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@DateTimeFormat(pattern="yyyy/MM/dd")
private DateTime birthDate; private DateTime birthDate;
@ManyToOne @ManyToOne

View file

@ -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.
*
* <p>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));
}
}

View file

@ -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);
}
}
}
}

View file

@ -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<PetType> {
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<PetType> findPetTypes = this.clinicService.findPetTypes();
for (PetType type : findPetTypes) {
if (type.getName().equals(text)) {
return type;
}
}
throw new ParseException("type not found: "+text, 0);
}
}

View file

@ -11,21 +11,6 @@
</layout> </layout>
</appender> </appender>
<logger name="org.springframework.web">
<level value="debug" />
</logger>
<logger name="org.springframework.jdbc">
<level value="info" />
</logger>
<logger name="javax.validation">
<level value="debug" />
</logger>
<logger name="org.hibernate.validator">
<level value="debug" />
</logger>
<!-- Root Logger --> <!-- Root Logger -->
<root> <root>

View file

@ -10,7 +10,7 @@
<script> <script>
$(function() { $(function() {
$("#birthDate").datepicker(); $("#birthDate").datepicker({ dateFormat: 'yy/mm/dd'});
}); });
</script> </script>
<div class="container"> <div class="container">
@ -44,7 +44,7 @@
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<label class="control-label">Birth Date (yyyy-MM-dd)</label> <label class="control-label">Birth Date</label>
<div class="controls"> <div class="controls">
<form:input path="birthDate" /> <form:input path="birthDate" />
<span class="help-inline"><form:errors path="birthDate" /></span> <span class="help-inline"><form:errors path="birthDate" /></span>

View file

@ -16,7 +16,7 @@
<context:component-scan base-package="org.springframework.samples.petclinic.web, org.springframework.samples.petclinic.service"/> <context:component-scan base-package="org.springframework.samples.petclinic.web, org.springframework.samples.petclinic.service"/>
<mvc:annotation-driven /> <mvc:annotation-driven conversion-service="conversionService"/>
<!-- all resources inside folder src/main/webapp/resources are mapped so they can be refered to inside JSP files <!-- all resources inside folder src/main/webapp/resources are mapped so they can be refered to inside JSP files
(see header.jsp for more details) --> (see header.jsp for more details) -->
@ -28,6 +28,15 @@
<mvc:view-controller path="/" view-name="welcome"/> <mvc:view-controller path="/" view-name="welcome"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="org.springframework.samples.petclinic.web.PetTypeFormatter" />
</set>
</property>
</bean>
<!-- <!--
- This view resolver delegates to the InternalResourceViewResolver and BeanNameViewResolver, - This view resolver delegates to the InternalResourceViewResolver and BeanNameViewResolver,
- and uses the requested media type to pick a matching view. When the media type is 'text/html', - and uses the requested media type to pick a matching view. When the media type is 'text/html',
@ -76,15 +85,6 @@
--> -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages/messages"/> p:basename="messages/messages"/>
<!--
Processes annotated handler methods, applying PetClinic-specific request parameter binding.
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.samples.petclinic.web.ClinicBindingInitializer"/>
</property>
</bean>
<!-- <!--
- This bean resolves specific types of exceptions to corresponding logical - This bean resolves specific types of exceptions to corresponding logical