2015-06-19 16 views
0

這個問題是涉及到:的Spring Portlet jQuery的阿賈克斯後JSON日期時間轉換錯誤

Spring Portlet Jquery Ajax post to Controller

我提交表單從Jquery的Ajax技術的Spring Portlet MVC控制器。問題是startTimestamp和endTimestamp是POJO中的dateTime。 json將其作爲字符串發送,轉換不會發生。錯誤是:

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 

我也不能編輯Pojo並添加@DateTimeFormat,因爲Pojos是從XSD生成的。我也嘗試添加一個customObjectMapper,但沒有任何工作。任何幫助將非常感激。

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

控制器:

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

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

請幫

回答

0

我能加入這個到控制器,以解決這一問題。我不得不這樣做的主要原因是它是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)); 
     }