Java 在SpringMVC控制器中使用jquery传递对象数组
我试图将文件数组对象传递给SpringMVC控制器。但是我收到了400个错误的请求 在我的jsp中,我编写了一个jquery方法来获取文件数组Java 在SpringMVC控制器中使用jquery传递对象数组,java,jquery,ajax,spring-mvc,Java,Jquery,Ajax,Spring Mvc,我试图将文件数组对象传递给SpringMVC控制器。但是我收到了400个错误的请求 在我的jsp中,我编写了一个jquery方法来获取文件数组 function getFilelist() { var files = $('body').data('filelist'); return files; } 并使用post Json方法上传这些文件 ---更新---- 我正在添加整个init()方法: 这里的getFilelist()是文件对象的数组。在我的控制器中,我写了
function getFilelist() {
var files = $('body').data('filelist');
return files;
}
并使用post Json方法上传这些文件
---更新----
我正在添加整个init()方法:
这里的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 + "]";
}
}
这是如果我添加两个图像对象的结果
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
}
]
}
您没有打印此处很重要的
{
、}
、[
和]
字符,但我假设它只是一个对象,而您的控制器需要一个文件附件
对象列表
使用数组return[files]在JS中添加环绕文件代码>或者,如果它始终仅在文件附件上,则将控制器更改为@RequestParam(“文件附件”)文件附件文件附件
编辑:
能否将@RequestParam(“文件附件”)
更改为@RequestParam(“文件名”)
?我看到你同时编辑了你的问题。你能从浏览器调试中添加什么数据结构(结果是getFilelist()
)吗?你好,我已经更新了我的问题。你好,如果你还需要什么,请告诉我。如果控制器中有字符串filesAttachedList,则可以使用另一个JSON反序列化程序Gson来提取数据:(new Gson()).fromJson(filesAttachedList,filesAttachedList.class)代码>。是的Xaerxess,非常感谢您的帮助。嗨Xaerxess,谢谢您的回复,它有不止一个文件附件,请查看我的更新。您可以更改@RequestParam(“filesAttachedList”)吗FileAttachedList FileAttachedList
到@RequestBody最终字符串FileAttachedList
,并在控制器的调试中检查FileAttachedList值是多少?给定更新,如果您将参数更改为@RequestParam(“文件名”)
,它应该可以工作。如果不是,这意味着您的FileAttachment
类在某种程度上存在缺陷,因为JSON是正常的,控制器接收数据,但MappingJacksonHttpMessageConverter
无法处理将JSON转换为FileAttachment
@Xaerxess这方面您能帮我吗
Array[2]
0: Object
1: Object
length: 2
__proto__: Array[0]
{
"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
}
]
}