我的应用程序有一个在客户端运行的websocket客户端,我正在使用websocket上传文件。
我有一个wicket WebPage,它有两种上传USB文件的方式(自动和输入类型文件) 当我的页面被实例化时,它的实例为“X”。 当wicket组件发出ajax请求时,我的页面有实例“X”。 当通过javascript代码发出请求时,处理发生在“X”中,但是在处理wicket之后创建新实例“Y”而没有在“X”中处理数据,并向用户显示实例Y.它总是在一个新的ajax(javascript代码)请求。
使用Javascript:
$.ajax({
url : postUrl,
type : 'post',
cache : false,
data :data,
processData: false,
contentType: false,
complete : function(xhr, status) {
$("#messagemWs").html("");
wsBu.stopBusca();
}
});
Ajax行为:
public class WsFilesAjaxBehavior extends AbstractDefaultAjaxBehavior {
private static final long serialVersionUID = 1L;
public static final String WS_CALBACK = "my:wsupload.callback";
private WsFilesAjaxBehaviorCallback callback;
public WsFilesAjaxBehavior(WsFilesAjaxBehaviorCallback callback) {
super();
this.callback = callback;
}
public void setCallback(WsFilesAjaxBehaviorCallback callback) {
this.callback = callback;
}
@Override
protected void onComponentTag(ComponentTag tag) {
tag.put(WS_CALBACK, getCallbackUrl().toString());
super.onComponentTag(tag);
}
private void buildFileUploadList(List<FileItem> fileItemsRequest, List<FileUpload> fileItems) {
for (FileItem fileItem : fileItemsRequest) {
fileItems.add(new FileUpload(fileItem));
}
}
private void getRequestFiles(final RequestCycle requestCycle, AjaxRequestTarget target) {
final ServletWebRequest webRequest = (ServletWebRequest) requestCycle.getRequest();
Map<String, List<FileItem>> multiPartRequestFiles = null;
try {
MultipartServletWebRequest multiPartRequest = webRequest.newMultipartWebRequest(Bytes.megabytes(1), "UploadInfo");
multiPartRequest.parseFileParts();
multiPartRequestFiles = multiPartRequest.getFiles();
} catch (FileUploadException e) {
e.printStackTrace(System.out);
return;
}
List<FileUpload> fileUploads = new ArrayList<>();
if (multiPartRequestFiles != null && !multiPartRequestFiles.isEmpty()) {
for (Entry<String, List<FileItem>> entry : multiPartRequestFiles.entrySet()) {
System.out.println(entry.getKey());
buildFileUploadList(entry.getValue(), fileUploads);
}
}
callback.webSocketCallback(fileUploads, target);
// target.appendJavaScript("alert('Processado!');");
}
@Override
protected void respond(AjaxRequestTarget target) {
RequestCycle requestCycle = RequestCycle.get();
getRequestFiles(requestCycle, target);
}
}
想法?
答案 0 :(得分:1)
您需要传递特殊的Wicket-Ajax请求参数。查看Wicket Ajax行为发送的确切名称。
AFAIR他们是:Wicket-Ajax = true和Wicket-Base-Url =某事。请参阅wicket-ajax-jquery.js以了解如何为第二个创建值。