我想将文件对象数组传递给Spring MVC控制器。但是我得到了400错误的请求错误。在spring中使用jquery传递对象数组MVC控制器
在我的jsp中,我写了一个jquery方法来获取文件数组。
function getFilelist() {
var files = $('body').data('filelist');
return files;
}
并使用后Json方法上传这些所有文件。
--- ----更新
我加入整个init()方法:
function init() {
$('input:button').button();
$('#submit').button();
$('#uploadForm').submit(function(event) {
event.preventDefault();
alert("uploading..");
$.postJSON('fileSave.htm', {
filename: getFilelist()
},
function(result) {
if (result.success == true) {
dialog('Success', 'Files have been uploaded!');
} else {
dialog('Failure', 'Unable to upload files!');
}
});
});
$('#reset').click(function() {
clearForm();
dialog('Success', 'Fields have been cleared!');
});
$('#upload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result, function (index, file) {
$('body').data('filelist').push(file);
$('#filename').append(formatFileDisplay(file));
$('#attach').empty().append('Add another file');
});
}
});
$("#attach").click(function() {
$("#upload").trigger('click');
});
$('body').data('filelist', new Array());
}
这里getFilelist()是文件对象的阵列。在我的控制器我已经写:
@RequestMapping(value="fileSave", method=RequestMethod.POST)
public @ResponseBody StatusResponse message(
@RequestParam("filesAttachedList") FilesAttachedList filesAttachedList) {
// some code here
}
我FilesAttachedList
类:
public class FilesAttachedList implements Serializable {
private static final long serialVersionUID = 5944305104394883501L;
private List<FileAttachment> filesList;
public List<FileAttachment> getFilesList() {
return filesList;
}
public void setFilesList(List<FileAttachment> filesList) {
this.filesList = filesList;
}
@Override
public String toString() {
return "FilesAttachedList [filesList=" + filesList + "]";
}
}
所以,我怎么能传递对象的数组从jQuery来控制器?
--updated--
当我检查浏览器getFileList返回
Object
attachableId: null
attachableType: null
attachmentContentType: null
attachmentFileName: "http://localhost:8080/myproject/resources/images.jpg"
attachmentFileSize: 6994
attachmentUpdatedAt: null
createdAt: null
creatorId: null
id: null
name: "images.jpg"
updatedAt: null
updaterId: null
__proto__: Object
length: 1
__proto__: Array[0]
这是什么,如果我添加了两个图像对象
Array[2]
0: Object
1: Object
length: 2
__proto__: Array[0]
----更新3 ---
当我从@RequestParam("filesAttachedList") FilesAttachedList filesAttachedList
更改为@RequestBody final String filesAttachedList
并添加两个图像时,它为我提供以下字符串对象。我可以读这个字符串吗?
{
"filename": [
{
"id": null,
"createdAt": null,
"updatedAt": null,
"name": "images.jpg",
"attachableId": null,
"attachableType": null,
"attachmentFileName": "http:\/\/localhost:8080\/myproject\/resources\/images.jpg",
"attachmentContentType": null,
"attachmentFileSize": 6994,
"attachmentUpdatedAt": null,
"creatorId": null,
"updaterId": null
},
{
"id": null,
"createdAt": null,
"updatedAt": null,
"name": "lion.jpg",
"attachableId": null,
"attachableType": null,
"attachmentFileName": "http:\/\/localhost:8080\/myproject\/resources\/lion.jpg",
"attachmentContentType": null,
"attachmentFileSize": 36670,
"attachmentUpdatedAt": null,
"creatorId": null,
"updaterId": null
}
]
}
你可以从浏览器调试中添加什么数据结构('getFilelist()')的结果? – Xaerxess
嗨Xaerxess,我已经更新了我的问题。 – user965884
您好Xaerxess,请让我知道如果您需要任何其他 – user965884