Announcement Announcement Module
Collapse
No announcement yet.
Registering Global PropertyEditors for Contollers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Registering Global PropertyEditors for Contollers

    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.

    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>
    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.

    Any advice would be greatly appreciated.

    Many thanks,
    Craig

  • #2
    Has anyone else tried to accomplish what I have been doing?

    I don't believe that what I am trying in unusual and would appreciate any guidance from anyone who has tried something similar.

    Thanks

    Comment


    • #3
      Me too.

      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.

      Comment


      • #4
        Submitting a JIRA issue and a fix is certainly a possibility and the solution should be easy enough to fix but i wanted to find out if other people have had similar problems with this.

        Out of curiosity did you solve your problem and if so, how?

        Comment


        • #5
          I have faced this situation as well, I have raised a JIRA issue for this.
          https://jira.springsource.org/browse/SPR-7077

          Comment

          Working...
          X