Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with Spring's default PropertyEditor search

 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Spring.

I want to create a custom PropertyEditor and found the following in Spring documentation

Spring uses the java.beans.PropertyEditorManager to set the search path for property editors that might be needed. The search path also includes sun.bean.editors, which includes PropertyEditor implementations for types such as Font, Color, and most of the primitive types. Note also that the standard JavaBeans infrastructure will automatically discover PropertyEditor classes (without you having to register them explicitly) if they are in the same package as the class they handle, and have the same name as that class, with 'Editor' appended;


I have a class which holds several enums as inner elements



I have 2 enumerated types Enumerated types Country & Curency
the class names are
com.anoop.spring.AllEnums$Country
and
com.anoop.spring.AllEnums$Currency


And correspondingly I have two Property Editors
CountryEditor and CurrencyEditor
com.anoop.spring.AllEnums$CountryEditor
and
com.anoop.spring.AllEnums$CurrencyEditor


Now I have a wrapper bean as given below


Now I have a MultiActionController with a method as given below



The problem is Spring is never using my PropertyEditors for setting the properties country and currency of the bean com.anoop.spring.Cash because of which I get exception with the following query string country=IN¤cy=INR
at the same time country=INDIA¤cy=RUPEE works fine

Could you please tell me what went wrong with my PropertyEditor definitions.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you register your editors within the CustomEditorConfigurer ?
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is not needed because The Editor is in the default search path of PropertyManager

My type class is com.anoop.spring.AllEnumClass$Country and its editor is com.anoop.spring.AllEnumClass$CountryEditor by convention it fits to the default search path where PropertyManager looks for the Property Editors.
This is what I found in spring official documentation on PropertyEditors


Spring uses the java.beans.PropertyEditorManager to set the search path for property editors that might be needed. The search path also includes sun.bean.editors, which includes PropertyEditor implementations for types such as Font, Color, and most of the primitive types. Note also that the standard JavaBeans infrastructure will automatically discover PropertyEditor classes (without you having to register them explicitly) if they are in the same package as the class they handle, and have the same name as that class, with 'Editor' appended;
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the problem .
It is the PropertyEditor as a inner class .
Property Manager is failing to create a new instance of the property editor when it is an inner class (I do not know why !!!)
but when I created a class with the name com.anoop.spring.AllEnums$CountryEditor it worked
[ February 04, 2008: Message edited by: Anoop Krishnan ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you're having a plain old Java problem here. With an inner class, you need to instanciate the class which contains the inner class first. You cannot do the following, and Spring neither :

Spring needs to instanciate AllEnums first, to do something like this :


To avoid this, I can think of at least two ways :
  • Don't make your enums as inner classes ! Put them in separate files.
  • Make the inner editors static. If they are statis, Spring will be able to access them.

  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic