2015-06-18 22 views
0

編輯:的Spring Portlet jQuery的阿賈克斯後到控制器

的開始日期和結束日期是喬達日期時間的POJO和我得到的錯誤是:

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,但沒有任何工作。任何幫助將非常感激。

原題:

我試圖提交表單和數據發送到控制器方法。問題是ModelAttribute是空的並且沒有值。 Spring MVC中的Portlet + JSP + JavaScript和jQuery的+控制器@ResourceMapping

段:

JSP:

<portlet:resourceURL id="addNewURL" var="addNewURL"> 
    </portlet:resourceURL> 

<form:form id="qmat_new_notification_form" action="#" method="POST" modelAttribute="dataObject">  
    ... 
    <input type="text" class="date-picker" id="start_date"> 
    ...  
    <input type="submit" value="Save" class="button" onclick="addNew()">  
</form:form> 

的Jquery:

function addNew() { 

      var dataObject = JSON.stringify({ 
       'startTime': $('#start_date').val(), 
       'endTime': $('#end_date').val(), 
       'description': $('#message').val(), 
       'active': $('#status').val() 
      }); 

      alert("data::"+dataObject); 

      $.ajax({ 
       url: "<%=addNewURL%>", 
       type: 'POST', 
       contentType: 'application/json', 
       data: dataObject 
      }).done(function(json){   
alert("Success!"); 
//more logic  
      }).fail(function() { 
       alert("OOPS!"); 
      }); 
     } 

控制器:

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

      if (!bindingResult.hasErrors()) { 
       System.out.println("a:::"+n.getDescription()); 
} 

此getDescription爲null。另外如果我使用request.getParameter(「description」)也是null。我錯過了什麼?請幫助

+0

哪個入口? Liferay 6.2? –

+0

Websphere Portal Server 7.0.0.2。感謝您的詢問 – Harry

回答

1

您根本不需要使用JSON數據。

首先,避免dataObject時的字串:

var dataObject = {...}; // no JSON.stringify call 

二,刪除contentType: 'application/json'因爲它沒有在這種情況下才有意義。

由於dataObject是一個鍵/值對,並且默認contentType,POST請求將被正確構建。

同時處理點擊提交事件,我建議jQuery的點擊和提交方式:

$("#submit").click(function (event) { 
    addNew(); 
    event.preventDefault(); 
}); 
$("#submit").submit(function (event) { 
    addNew(); 
    event.preventDefault(); 
}); 

我已經創建了問題的fiddle

請參閱jQuery.ajax文檔。

+0

非常感謝。這幾乎可行!但問題是,當我使用submit時,javascript函數不會被調用。當我在提交按鈕上使用onclick時它確實會被調用。但它不去控制器!!同樣在形式上應該是行動的價值。我可以將它設置爲$ {addNewURL}嗎?請指教。謝謝 – Harry

+0

我會將其標記爲正確,因爲它解決了我的問題的一部分。將爲dateTime問題發佈另一個問題。謝謝 – Harry

+0

謝謝。我更新了答案,以涵蓋表單中的點擊和提交事件。 –