Spring Portlet Jquery Ajax发布json dateTime转换错误

时间:2015-06-19 19:58:10

标签: jquery json spring jodatime spring-portlet-mvc

这个问题与:

有关

Spring Portlet Jquery Ajax post to Controller

我将表单从Jquery Ajax提交到Spring Portlet MVC Controller。问题是startTimestamp和endTimestamp是POJO中的dateTime。 json将其作为字符串发送,并且不会发生转换。错误是:

    frameWidth = self.view.frame.size.width
    self.videosView.rowHeight = UITableViewAutomaticDimension
    self.videosView.estimatedRowHeight = frameWidth

我也无法编辑Pojo并添加@DateTimeFormat,因为Pojos是从XSD生成的。我也尝试添加一个customObjectMapper,但没有任何作用。任何帮助将不胜感激。

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

控制器:

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.");
            });
}

请帮忙

1 个答案:

答案 0 :(得分:0)

我能够通过将此添加到Controller来解决问题。我必须这样做的主要原因是它是Portlet MVC,我无法为POJO添加注释,因为它们是从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));
       }