2012-08-23 3 views
0

파일 배열을 Spring MVC 컨트롤러에 전달하려고합니다. 하지만 400 개의 잘못된 요청 오류가 발생합니다.봄 MVC 컨트롤러에서 jquery를 사용하여 객체 배열 전달

내 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이 내가 두를 추가하면 이미지가

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

개체를 무엇

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] 

반환 ---- 업데이트 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("filename")-@RequestParam("fileAttachment")를 변경할 수 있습니까? 나는 당신이 당신의 질문을 편집 한 것을 봅니다.

+0

안녕하세요 Xaerxess, 답장을 보내 주셔서 감사합니다. 첨부 파일이 두 개 이상인 경우 내 업데이트를 확인하십시오. – user965884

+0

두 이미지가 작동합니까? – Xaerxess

+0

No Xaeress가 이미지 수에 상관없이 작동하지 않습니다. – user965884

관련 문제