如何在RPC中使用相同的servlet并在GWT中上传文件。

时间:2012-08-24 11:00:50

标签: gwt servlets gwt-rpc gwt2 formpanel

我创建了一个Web应用程序,我必须使用fileUpload。

我必须将文件及其属性发送到服务器。对于发送文件我使用FormPanel和我使用RPC的属性。

public void onModuleLoad() {
    final FileServiceEndPoint serviceEndPoint = new FileServiceEndPoint();
    new AddDocument();
    Button b = new Button("addDocument");
    b.addClickHandler(new ClickHandler() {
        private Map<String, String> docProperty;
        public void onClick(ClickEvent event) {
            docProperty =getProperties();
            AsyncCallback<String> callback = new AsyncCallback<String>() {
                public void onSuccess(String result) {
                    System.out.println("he ha" +result);
                }
                public void onFailure(Throwable caught) {

                }
            };
            serviceEndPoint.uploadAttachement(docProperty, callback);
        }
    });
    RootPanel.get().add(b);
}

这个新的AddDocument();包含上传文件的代码(formPanel代码)

private FormPanel getFormPanel() {
        if (uploadForm == null) {
            uploadForm = new FormPanel();
            uploadForm.setAction(GWT.getHostPageBaseURL() +"TestUploadFileServlet");
            uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
            uploadForm.setMethod(FormPanel.METHOD_POST);
            uploadForm.setWidget(getFileUpload());

            System.out.println(GWT.getHostPageBaseURL() +"TestUploadFileServlet");

            uploadForm.addFormHandler(new FormHandler() {

                public void onSubmitComplete(FormSubmitCompleteEvent event) {
                    AddDocument.this.hide(true);        
                }

                public void onSubmit(FormSubmitEvent event) {
                }
            });


        }
        return uploadForm;
    }

private Button getAddButton() {
        if (addButton == null) {
            addButton = new Button("ADD");
            addButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    uploadForm.submit();
                }
            });
            addButton.setText("Add");

        }

为Sending属性创建接口。

端点:

public class FileServiceEndPoint implements FileServiceAsync{

FileServiceAsync service = (FileServiceAsync)GWT.create(FileService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) service;

public FileServiceEndPoint() {
endpoint.setServiceEntryPoint(GWT.getHostPageBaseURL() + “TestUploadFileServlet”);
}

public void uploadAttachement(Map docProperty,
AsyncCallback callback) {
service.uploadAttachement(docProperty, callback);
}
}

在服务器上:

public class FileUploadImpl extends RemoteServiceServlet implements FileService {

private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(FileUploadImpl.class
.getName());

String a;
protected void service(final HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {

a=”5″;
System.out.println(“ServletWorking Fine “);
}
public String uploadAttachement(Map docProperty) {
// TODO Auto-generated method stub
return “Checked”;
}
}

当我调试formPanel.submit时:调试器进入服务器并打印ServletWorking Fine(这是完美的

当我调试addProperties按钮时,它转到服务器并打印ServletWorking Fine。但它不应该采用服务方法。 调试器应该进入UploadAttachement。

Plz告诉如何使用相同的servlet传递hashMap。

0 个答案:

没有答案