Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<mvc:annotation-driven /> doesn't allow CustomPropertyEditor be co-exists [SPR-7077] #11737

Closed
spring-issuemaster opened this issue Apr 7, 2010 · 8 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Apr 7, 2010

Simon Wong opened SPR-7077 and commented

Although it is preferable to use conversionService rather than use the CustomEditor, in some situation, custom editor is still required (e.g. StringTrimmerEditor). The current AnnotationDrivenBeanDefinitionParser (used by MvcNamespaceHandler for <mvc:annotation-driven />) will create a ConfigurableWebBindingInitializer instance internally, which doesn't allow to register custom editors with propertyEditorRegistrars.


Affects: 3.0 GA, 3.0.1, 3.0.2

Reference URL: http://forum.springsource.org/showthread.php?t=84267

Issue Links:

  • #13290 Declaring RequestMappingHandlerAdapter before makes Spring ignore my custom WebBindingInitializer

7 votes, 12 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 29, 2011

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Sep 12, 2011

Rossen Stoyanchev commented

We can consider supporting a propertyEditorRegistrar attribute on <annotation-driven />. A workaround which you might be aware of is using a BeanPostProcessor (code sample) or Java config in Spring 3.1 (code sample).

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 7, 2012

Canny Duck commented

The link to the BeanPostProcessor code sample is broken. Can you please fix it? I want to register StringTrimmerEditor without Java config using the XML-style.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 7, 2012

Rossen Stoyanchev commented

I've corrected the link to the sample above. See the JacksonConversionServiceConfigurer class but keep in mind you might have to check for a bean of instance RequestMappingHandlerAdapter rather than AnnotationMethodHandlerAdapter depending on which one you're actually using.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 8, 2012

Canny Duck commented

Thanks for your quick reply. I would be glad if you can show me a little code example setting the StringTrimmerEditor as PropertyEditor on the RequestMappingHandlerAdapter.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 11, 2012

Canny Duck commented

This code unfortunately does not work. Any ideas?

public class DefaultBindingConfigurer implements BeanPostProcessor
{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
{
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
{
    if (bean instanceof RequestMappingHandlerAdapter)
    {
        RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean;
        WebBindingInitializer initializer = adapter.getWebBindingInitializer();
        if (initializer instanceof ConfigurableWebBindingInitializer)
        {
            ConfigurableWebBindingInitializer configurableInitializer = (ConfigurableWebBindingInitializer) initializer;
            configurableInitializer.setPropertyEditorRegistrar(new StringTrimmerRegistrar());
        }
    }
    return bean;
}

private class StringTrimmerRegistrar implements PropertyEditorRegistrar
{
    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry)
    {
        registry.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
}

}

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 11, 2012

Rossen Stoyanchev commented

The forum would be a more appropriate place for this type of discussion. Please, post a question there and provide some detail around "doesn't work".

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Aug 20, 2012

Rossen Stoyanchev commented

@ControllerAdvice now available in master (and starting Spring 3.2 M2) allows defining a global @InitBinder method, which makes it possible to register PropertyEditor's in one place globally. The related issue is #13750.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.