【问题标题】:EXTJS and Spring MVC Portlet File UploadEXTJS 和 Spring MVC Portlet 文件上传
【发布时间】:2011-10-26 21:16:53
【问题描述】:

我已经成功地将文件上传到我的控制器中的 ActionResponse 方法,用于使用 extjs 的 spring portlet。我的问题是 extjs 需要一个 json 响应才能知道上传是否完成,而我的操作请求的响应会呈现整个门户。

我需要一种方法来告诉我的请求的响应只是一个 json 字符串。

这是我的控制器。

@Controller

@RequestMapping(value = "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");
                            }
                        });
                    }
                }
            }]
        });

【问题讨论】:

    标签: spring model-view-controller file-upload extjs portlet


    【解决方案1】:

    这里有几种方法:


    1. 手动强制响应为这样的文本

      response.setContentType("text/html"); //如果你愿意,也可以是json

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

      response.getWriter().write(responseText);

    2. 使用此处介绍的 Jackson 库: Spring 3.0 making JSON response using jackson message converter

    3. 继续使用 Grails,忘记冗长的 MVC 配置。 我的首选方法 :)

    【讨论】:

    • 1.没有办法在动作响应上设置内容类型,ActionResponse 甚至没有写入响应。 2. 将我的结果转换为 json 不是问题,我需要我的结果不呈现整个门户包装器。 3.这不是我做的决定,spring mvc是企业标准。
    • 没有意识到您正在使用 Portal!
    猜你喜欢
    • 2013-01-20
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多