Spring Portlet Jquery Ajax post json 日期时间转换错误
Spring Portlet Jquery Ajax post json dateTime conversion error
这个问题是关于:
我正在从 Jquery Ajax 向 Spring Portlet MVC 控制器提交表单。问题是 startTimestamp 和 endTimestamp 是 POJO 中的日期时间。 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,因为 Pojo 是从 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()) {
...
}
}
请帮忙
我可以通过将其添加到控制器来解决问题。我必须这样做的主要原因是它是 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));
}
这个问题是关于:
我正在从 Jquery Ajax 向 Spring Portlet MVC 控制器提交表单。问题是 startTimestamp 和 endTimestamp 是 POJO 中的日期时间。 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,因为 Pojo 是从 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()) {
...
}
}
请帮忙
我可以通过将其添加到控制器来解决问题。我必须这样做的主要原因是它是 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));
}