我已经成功地使用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");
}
});
}
}
}]
});
1.没有办法设置内容类型上的动作响应,ActionResponse的不具有反正甚至书面方式来响应的。 2.我的结果转换为json不是问题,我需要我的结果不呈现整个门户封装。 3.这不是我做出的决定,spring mvc是一个企业标准。 – user1015434
没有意识到你正在使用一个门户网站! – dbrin