0

ファイルオブジェクトの配列をSpringMVCコントローラーに渡そうとしています。しかし、400Badリクエストエラーが発生します。

私のjspでは、ファイルの配列を取得するためのjqueryメソッドを作成しました。

function getFilelist() {
      var files = $('body').data('filelist');
      return files;
}

そして、postJsonメソッドを使用してこれらすべてのファイルをアップロードします。

- -更新しました - -

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からコントローラーに渡すことができますか?

- 更新しました -

ブラウザをチェックすると、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]

これは、2つの画像オブジェクトを追加するとどうなりますか

 Array[2]
 0: Object
 1: Object
 length: 2
 __proto__: Array[0]

----アップデート3---

からに変更し@RequestParam("filesAttachedList") FilesAttachedList filesAttachedList@RequestBody final String filesAttachedList2つの画像を追加すると、次の文字列オブジェクトが表示されます。この文字列を読み取れますか?

{
  "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
    }
  ]
}
4

1 に答える 1

1

{ここで重要な、}[および文字を出力しませんでした]が、コントローラーがオブジェクトのリストを期待している間、それは 1 つのオブジェクトにすぎないと想定していFileAttachmentます。

配列を使用してJSにサラウンドファイルを追加するreturn [ files ];か、常にFileAttachmentコントローラーを変更する場合は@RequestParam("fileAttachment") FileAttachment fileAttachment.

編集:

に変更できます@RequestParam("fileAttachment")@RequestParam("filename")?その間、あなたが質問を編集したようです。

于 2012-08-23T16:46:25.393 に答える