2011-10-26 14 views
0

extjsを使用しているSpringポートレットのコントローラで、ActionResponseメソッドにファイルを正常にアップロードできました。私の問題は、アップロードが完了したことを知るためにextjsがjsonレスポンスを期待しており、アクションリクエストのレスポンスがポータル全体をレンダリングすることです。EXTJSとSpring MVCポートレットファイルのアップロード

私のリクエストが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

アップロードコードをファイルです。


この

response.setContentType( "テキスト/ HTML")のようなテキストとして10

  1. 手動力応答。 //希望の場合はjson

    responseText = "{'success': 'true'}";

    response.getWriter()。write(responseText);ここではカバーされて

  2. 使用ジャクソンのlib: Spring 3.0 making JSON response using jackson message converter

  3. のGrailsに移動し、詳細なMVCのコンフィグ忘れます。 私の好ましい方法:)

+0

1.行動応答にコンテンツタイプを設定する方法はありません、ActionResponseはとにかくも、レスポンスに足すのはありません。 2.結果をjsonに変換することは問題ではありません。ポータルラッパー全体をレンダリングしないために結果が必要です。 3.これは私の判断ではなく、spring mvcは企業標準です。 – user1015434

+0

あなたはポータルを使用しているとは思わなかった! – dbrin