【问题标题】:Pass array of objects using jquery in spring MVC controller在 Spring MVC 控制器中使用 jquery 传递对象数组
【发布时间】:2012-08-19 05:26:38
【问题描述】:

我正在尝试将文件对象数组传递给 Spring MVC 控制器。但我收到 400 Bad request 错误。

在我的 jsp 中,我编写了一个 jquery 方法来获取文件数组。

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

并使用 post 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 传递到控制器?

--更新--

当我检查浏览器时 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,如果您还需要什么,请告诉我
  • P.S.如果您的控制器中有字符串 filesAttachedList,您可以使用另一个 JSON 反序列化器 Gson 来提取数据:( new Gson() ).fromJson( filesAttachedList, FilesAttachedList.class );。
  • 是的,Xaerxess,非常感谢您的帮助。

标签: java jquery ajax spring-mvc


【解决方案1】:

您没有打印在这里很重要的{、}、[ 和] 字符,但我认为它只是一个对象,而您的控制器需要一个FileAttachment 对象的列表。

要么在 JS 中添加带有数组 return [ files ]; 的环绕文件,或者,如果它始终只在 FileAttachment 上,则将控制器更改为 @RequestParam("fileAttachment") FileAttachment fileAttachment。

编辑:

你能把@RequestParam("fileAttachment")改成@RequestParam("filename")吗?我看到你同时编辑了你的问题。

【讨论】:

  • 嗨,Xaerxess,感谢您的回复,它的文件不止一个,请查看我的更新
  • 那么您可以将@RequestParam("filesAttachedList") FilesAttachedList filesAttachedList 更改为@RequestBody final String filesAttachedList 并在您的Controller 中进行调试检查filesAttachedList 的值是什么?
  • 鉴于更新,似乎如果您将参数更改为 @RequestParam("filename") 它应该可以工作。如果不是,那意味着您的 FileAttachment 类在某种程度上存在缺陷,因为 JSON 正常,控制器接收数据但 MappingJacksonHttpMessageConverter 无法处理将 JSON 转换为 FileAttachment。
  • @Xaerxess 你能帮我解决这个问题吗stackoverflow.com/questions/18102452/…
猜你喜欢
  • 2017-11-25
  • 2017-03-17
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
相关资源
最近更新 更多