Hi there,
I've been trying for the last couple of days to try and register a custom property editor that is applied globbaly and is avaiable to all my controllers without the need to register it in every controller.
I have managed to do this but my app-servlet.xml file now creates the all the beans that would have been created by using the <mvc:annotation-driven/> tag which creates the annotation handlers and adds in the support for JSR303 validation via Hibernate Validator, purely so i can registor a custom propertyEditorRegistrars.
With the CustomPropertyEditorRegistrar class as follows which registers a editor which is used to convert timestamps to string formatted dates for the frontend:Code:<!--mvc:annotation-driven/--> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean id="configurableWebBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService"> <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/> </property> <property name="validator"> <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> </property> <property name="propertyEditorRegistrars"> <list> <bean class="my.package.CustomPropertyEditorRegistrar"/> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer" ref="configurableWebBindingInitializer" /> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> </list> </property> </bean>
Note: I am aware of DateEditor that already exists but we have made the decision to use timestamps throughout the application and use them for precision when required.Code:public final class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { // it is expected that new PropertyEditor instances are created registry.registerCustomEditor(Timestamp.class, new CustomTimestampEditor(new SimpleDateFormat("dd/MM/yyyy"), false, 10)); } }
As you can in the app-sevlet.xml is that one line compared to the 25 lines I had to add to just add in a global property editor.
I tried using the CustomEditorConfigurer but this does not add to the form controllers, also tried creating my own WebBindingInitialiser but this would then would replace the default one which registers the conversion service and validation whiich i do not want to manually create.
I hope you see my dilemma here as i want to use the mvc namespace to register the beans and then register a custom editor (which neeeds to be added in the ConfigurableWebBindingInitializer) but think what I have done is overkill to achieve this.
Any advice would be greatly appreciated.
Many thanks,
Craig


Reply With Quote
