2011-10-26 61 views
0

我已經成功地使用extjs將我的控制器中的文件上傳到我的ActionResponse方法中。我的問題是,extjs期待一個json響應,以便知道上傳已完成,並且我的操作請求的響應呈現出整個門戶。EXTJS和Spring MVC Portlet文件上傳

我需要一種方式來告訴響應我的請求只是一個JSON字符串。

這裏是我的控制器。

@Controller 

@RequestMapping(值= 「VIEW」) 公共類MediaRoomViewController {

private static final Logger _log = Logger.getLogger(MediaRoomViewController.class); 

@RenderMapping 
public String renderView(RenderRequest request, RenderResponse response, Model model){ 
    return "view"; 
} 

@InitBinder 
protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) throws Exception { 
    // to actually be able to convert Multipart instance to byte[] 
    // we have to register a custom editor 
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); 
    // now Spring knows how to handle multipart object and convert 
} 

@ActionMapping(params="action=uploadFile") 
public String uploadFile(FileUploadBean uploadItem, BindingResult result,ActionResponse response) throws Exception { 
    _log.debug("Upload File Action"); 

    ExtJSFormResult extjsFormResult = new ExtJSFormResult(); 
    try{ 
     if (result.hasErrors()){ 
      for(ObjectError error : result.getAllErrors()){ 
       System.err.println("Error: " + error.getCode() + " - " + error.getDefaultMessage()); 
      } 

      //set extjs return - error 
      extjsFormResult.setSuccess(false); 
     } 

     // Some type of file processing... 
     System.err.println("-------------------------------------------"); 
     System.err.println("Test upload: " + uploadItem.getFile().getOriginalFilename()); 
     System.err.println("-------------------------------------------"); 

     //set extjs return - sucsess 
     extjsFormResult.setSuccess(true); 
    }catch(Exception ex){ 
     _log.error(ex,ex); 
    } 
    return extjsFormResult.toString(); 
} 

這裏是我的ExtJS的文件上傳代碼

Ext.create('Ext.form.Panel', { 
      title: 'File Uploader', 
      width: 400, 
      bodyPadding: 10, 
      frame: true, 
      renderTo: self._Namespace + 'file-upload',  
      items: [{ 
       xtype: 'filefield', 
       name: 'file', 
       fieldLabel: 'File', 
       labelWidth: 50, 
       msgTarget: 'side', 
       allowBlank: false, 
       anchor: '100%', 
       buttonText: 'Select a File...' 
      }], 

      buttons: [{ 
       text: 'Upload', 
       handler: function() { 
        var form = this.up('form').getForm(); 
        if(form.isValid()){ 
         form.submit({ 
          url: self._Urls[0], 
          waitMsg: 'Uploading your file...', 
          success: function(form,action) { 
           alert("success"); 
          }, 
          failure: function(form,action){ 
           alert("error"); 
          } 
         }); 
        } 
       } 
      }] 
     }); 

回答

0

有幾種方法去這裏:


  1. 手動力響應作爲這樣

    response.setContentType( 「text/html的」)文本; //或json如果你願意

    responseText =「{'success':'true'}」;

    response.getWriter()。write(responseText);

  2. 使用傑克遜的lib爲這裏介紹: Spring 3.0 making JSON response using jackson message converter

  3. 轉移到Grails和忘記冗長MVC CONFIGS。 我的優選方法:)

+0

1.沒有辦法設置內容類型上的動作響應,ActionResponse的不具有反正甚至書面方式來響應的。 2.我的結果轉換爲json不是問題,我需要我的結果不呈現整個門戶封裝。 3.這不是我做出的決定,spring mvc是一個企業標準。 – user1015434

+0

沒有意識到你正在使用一個門戶網站! – dbrin