2012-08-23 78 views
0

我想将文件对象数组传递给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 
    } 
    ] 
} 
+0

你可以从浏览器调试中添加什么数据结构('getFilelist()')的结果? – Xaerxess

+0

嗨Xaerxess,我已经更新了我的问题。 – user965884

+0

您好Xaerxess,请让我知道如果您需要任何其他 – user965884

回答

1

你没有打印{}[]字符,这是很重要的位置,但我相信这只是一个对象,而你的控制器期望FileAttachment对象的列表。

要么将​​JS中的环绕文件添加到数组return [ files ];中,要么只是将FileAttachment更改控制器更改为@RequestParam("fileAttachment") FileAttachment fileAttachment

编辑:

可以更改@RequestParam("fileAttachment")@RequestParam("filename")?我看到你在编辑你的问题的同时。

+0

嗨Xaerxess,感谢您的回复,其超过1个文件附件,请检查我的更新 – user965884

+0

它是否适用于2张图片? – Xaerxess

+0

没有Xaeress它没有与任何数量的图像 – user965884

相关问题