jqueryjsonspringjodatimespring-portlet-mvc

Spring Portlet Jquery Ajax post json dateTime conversion error


This question is related to:

Spring Portlet Jquery Ajax post to Controller

I'm submitting a form from Jquery Ajax to Spring Portlet MVC Controller. The issue is startTimestamp and endTimestamp are dateTime in the POJO. The json sends it as a string and the conversion is not happeinng. The error is:

SystemOut     O 14:10:16.040 [WebContainer : 2] DEBUG org.springframework.beans.BeanUtils - No property editor [org.joda.time.DateTimeEditor] found for type org.joda.time.DateTime according to 'Editor' suffix convention
    ...
SystemOut     O Error::Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'startTimestamp'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTime] for property 'startTimestamp': no matching editors or conversion strategy found

I also can't edit the Pojo and add @DateTimeFormat because the Pojos are generated from XSD. I also tried adding a customObjectMapper, but nothing works. Any help would be much appreciated.

function addNew() {

            var startDateEle = $('#start_date').val();
            var startTimeEle = document.getElementById("start_time");
            var startTime = startTimeEle.options[startTimeEle.selectedIndex].value;
            var startDate = new Date(startDateEle + ' ' +startTime);

            var endDateEle = $('#end_date').val();
            var endTimeEle = document.getElementById("end_time");
            var endTime = endTimeEle.options[endTimeEle.selectedIndex].value;
            var endDate = new Date(endDateEle + ' ' +endTime);

            var dataObject = {
                    'startTimestamp': startDate,
                    'endTimestamp': endDate,
                    'description': $('#0_message').val(),
                    'active': $("input[name=status]:checked").val()
                };
            $.ajax({
                url: "<%=addNewURL%>",
                type: 'POST',
                data: dataObject
            }).done(function(json){
                console.log(json);  
                //alert("json::"+json);
                alert("Success!");
            }).fail(function() {
                alert("OOPS! An error occured while processing your request. Please try again after sometime.");
            });
}

Controller:

@ResourceMapping(value = "addNewURL")
    public void addNew(@ModelAttribute(value = "dataObject") CustObj n,
                        BindingResult bindingResult, ResourceRequest request, ResourceResponse response, ModelMap model) throws Exception {

        if (!bindingResult.hasErrors()) {
        ...
         }
}

Please help


Solution

  • I was able to solve the issue by adding this to the Controller. The main reason I had to do this was it was Portlet MVC and I couldn't add annotation to the POJO as they were generated from XSD:

           @InitBinder
           public void initBinder(WebDataBinder binder) {
                 binder.initDirectFieldAccess();
                 /* register appropriate date editor */
                String dateFormat = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z";
                binder.registerCustomEditor(DateTime.class, new DateTimeEditor(dateFormat, false));
           }