This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Registering Global PropertyEditors for Contollers

Feb 5th, 2010, 05:05 AM

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:

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

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.

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.

Comment

I've just run into the same problem as you. Sadly I don't have an answer, but I'll watch this thread to see if anyone else does. You might need to log a JIRA issue about making it easier to register global property editors.