Announcement Announcement Module
Collapse
No announcement yet.
PropertyEditorSupport setter and getter is not called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyEditorSupport setter and getter is not called

    Hi

    I am trying to create a CutomPropertyEditorSupport but the getAsText() and setAsText() are not called. I am using Spring 2.5 not the Spring 3.

    Here is my code:

    Code:
        import java.beans.PropertyEditorSupport;  
          
        public class StringToDateStringConvertorEditior extends PropertyEditorSupport {  
            @Override  
            public void setAsText(String text)  
                    throws java.lang.IllegalArgumentException {  
                System.out.println("Editors  Setter is calledd.........");  
                String modifiedValue = text.substring(0, 1) + "-"  
                        + text.substring(2, 3) + "-" + text.substring(4);  
              
                this.setValue(modifiedValue);  
            }  
            @Override  
            public String getAsText() {  
                System.out.println("Editors  Getter is calledd.........");  
                return (String) this.getValue();  
          
                }  
        }
    Adding My Editor using initBinder
    Code:
     protected void initBinder(HttpServletRequest request,  
                ServletRequestDataBinder binder) throws Exception {  
            System.out.println("Binder Init Binder...........");  
            binder.registerCustomEditor(StringToDateStringConvertorEditior.class,  
                    new StringToDateStringConvertorEditior());  
        }
    The Sysout of initBinder is coming but the sysout of propertyEditor is not coming. So I think the setAsText method is not called. Please help me

  • #2
    Originally posted by gouravbansal11 View Post
    Hi

    I am trying to create a CutomPropertyEditorSupport but the getAsText() and setAsText() are not called. I am using Spring 2.5 not the Spring 3.

    Here is my code:

    Code:
        import java.beans.PropertyEditorSupport;  
          
        public class StringToDateStringConvertorEditior extends PropertyEditorSupport {  
            @Override  
            public void setAsText(String text)  
                    throws java.lang.IllegalArgumentException {  
                System.out.println("Editors  Setter is calledd.........");  
                String modifiedValue = text.substring(0, 1) + "-"  
                        + text.substring(2, 3) + "-" + text.substring(4);  
              
                this.setValue(modifiedValue);  
            }  
            @Override  
            public String getAsText() {  
                System.out.println("Editors  Getter is calledd.........");  
                return (String) this.getValue();  
          
                }  
        }
    Adding My Editor using initBinder
    Code:
     protected void initBinder(HttpServletRequest request,  
                ServletRequestDataBinder binder) throws Exception {  
            System.out.println("Binder Init Binder...........");  
            binder.registerCustomEditor(StringToDateStringConvertorEditior.class,  
                    new StringToDateStringConvertorEditior());  
        }
    The Sysout of initBinder is coming but the sysout of propertyEditor is not coming. So I think the setAsText method is not called. Please help me

    Do I need to add something in my *-servlet.xml file

    Comment


    • #3
      I suggest a read of the reference guide?

      Code:
      binder.registerCustomEditor(StringToDateStringConvertorEditior.class, new StringToDateStringConvertorEditior());
      What you basically say here is for fields of type StringToDateStringConvertorEditior use this instance of the StringToDateStringConvertorEditior. I'm pretty sure your domain object doesn't have a field of type StringToDateStringConvertorEditior.

      To me it just seems like you ar abusing a string field to store a date and try to add some formatting. Why not simply use a field of type date and use the appropriate Editor to convert a date to a string and vice versa?!

      Comment


      • #4
        Thanks Marten,

        Yes i have the date as a string data type and want to do some formatting. I changed my code now to
        Code:
        binder.registerCustomEditor(String.class, new StringToDateStringConvertorEditior());
        and it is working fine

        Comment

        Working...
        X