I recently converted my java.util.Date fields to Jodatime DateTimes. My PropertyEditor for Dates worked fine. I am registering the new PropertyEditor in the same way, but I am getting an error:
Failed to convert property value of type [java.lang.String] to required type [org.joda.time.DateTime] for property 'startDate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTime] for property 'startDate': no matching editors or conversion strategy found
I've done a lot of searching to try to figure this problem out. As best I can tell, I am registering my PropertyEditor correctly:
Since that looks right, I looked to my PropertyEditor. The only relevant methods are the constructor, and setAsText(). They look correct to me. The PropertyEditor I am using, which I found elsewhere in the forum, is included below.Code:protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { String dateFormat = "MM/dd/yyyy"; binder.registerCustomEditor(org.joda.time.DateTime.class, new DateTimeEditor(dateFormat,true)); binder.bind(request); }
I've been trying to figure this out for several hours now. I would greatly appreciate any guidance in solving this problem.Code:public class DateTimeEditor extends PropertyEditorSupport { // ------------------------------ FIELDS ------------------------------ private final DateTimeFormatter formatter; private final boolean allowEmpty; // --------------------------- CONSTRUCTORS --------------------------- /** * Create a new DateTimeEditor instance, using the given format for * parsing and rendering. * <p/> * The "allowEmpty" parameter states if an empty String should be allowed * for parsing, i.e. get interpreted as null value. Otherwise, an * IllegalArgumentException gets thrown. * * @param dateFormat DateFormat to use for parsing and rendering * @param allowEmpty if empty strings should be allowed */ public DateTimeEditor( String dateFormat, boolean allowEmpty ) { this.formatter = DateTimeFormat.forPattern( dateFormat ); this.allowEmpty = allowEmpty; } // ------------------------ OVERRIDING METHODS ------------------------ /** * Format the YearMonthDay as String, using the specified format. * * @return DateTime formatted string */ public String getAsText() { Date value = ( Date ) getValue(); return value != null ? new DateTime( value ).toString( formatter ) : ""; } /** * Parse the value from the given text, using the specified format. * * @param text * @throws IllegalArgumentException */ public void setAsText( String text ) throws IllegalArgumentException { if ( allowEmpty && !StringUtils.hasText( text ) ) { // Treat empty String as null value. setValue( null ); } else { setValue( new DateTime( formatter.parseDateTime( text ) ).toDate() ); } } }


Reply With Quote