GXT文件上传不调用servlet

时间:2013-07-02 16:45:48

标签: java java-ee servlets gxt

我正在使用这个课程:

/**
 * 
 */
package com.xerox.tclg.ejuror.ui.client;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FormEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Image;
import com.xerox.tclg.ejuror.ui.client.service.PersonViewService;
import com.xerox.tclg.ejuror.ui.client.service.PersonViewServiceAsync;
import com.xerox.tclg.ejuror.ui.client.workbench.StyleHelper;

/**
 * Form for uploading images to the main server
 * @author Xerox Services
 */
public class UploadForm extends MessageEnabledContainer {
  //instance variables
  private String                  description;
  private String                  cancel;
  private String                  file;
  private MessageResource         messageResource;
  private String                  ok;
  private PersonViewServiceAsync  personViewService = (PersonViewServiceAsync) GWT.create(PersonViewService.class);

  //UI components
  private Button                  btnOk;
  private Button                  btnCancel;
  private ButtonBar               buttonBar;
  private LabelField              lblTitle;
  private FormPanel               form;
  private LayoutContainer         header;
  private LayoutContainer         layMain;
  private TextField<String>       textDescription;
  private FileUploadField         uploadFile;

    /**
   * Create a disclaimer page that will show disclaimers and return the results in a callback
     * @param icon
     * @param header1
     * @param header2
     * @param disclaimerText
     * @param buttonFlags
     */
    public UploadForm() {
    String    selectFile;
    String    title;
    messageResource = ImageViewer.getImageViewer();
    if(isDesignTime()){
      cancel = "Cancel";
      description = "Short Description<BR/>(e.g. Doctor's Letter)";
      file = "File";
      ok = "Ok";
      selectFile = "Select a File to Upload";
      title = "Upload a File";
    }
    else{
      cancel = messageResource.getString(Messages.MSGKEY_CANCEL);
      description = "Short Description<BR/>(e.g. Doctor's Letter)";
      file = "File";
      ok = messageResource.getString(Messages.MSGKEY_OK);
      selectFile = "Select a File to Upload";
      title = "Upload a File";
    }

    addStyleName("white-screen");
    setWidth(MainLayout.FORM_WIDTH);  
        setLayout(new TableLayout());

        header = new LayoutContainer();
        TableLayout tl_header = new TableLayout(2);
        tl_header.setCellSpacing(5);
        header.setLayout(tl_header);

    Image imageUpload = new Image(EjurorBundle.BUNDLE.uploadIcon());
    header.add(imageUpload);

    String str = StyleHelper.formatHeader(title);
    lblTitle = new LabelField(str);
    header.add(lblTitle);

    layMain = new LayoutContainer();
    TableLayout tl_layMain = new TableLayout();
    tl_layMain.setCellPadding(5);
    layMain.setLayout(tl_layMain);

    layMain.add(header);

    form = new FormPanel();
    form.setHeading(selectFile);  
    layMain.add(form);

    buildUploadForm();

    buildButtonBar();

    FooterContainer footer = new FooterContainer("50");
    TableData td_footer = new TableData();
    td_footer.setWidth("100%");
    layMain.add(footer, td_footer);
    footer.setSize("740", "");

    add(layMain);

    loadUrl();
    }

  /**
   * 
   */
  private void buildButtonBar() {
//    buttonBar = new ButtonBar();
//    buttonBar.setAlignment(HorizontalAlignment.RIGHT);
//    buttonBar.setHeight(MainLayout.BUTTON_BAR_HEIGHT);
//    form.add(buttonBar);

    btnOk = new Button(ok);
    btnOk.setSize(MainLayout.BUTTON_WIDTH, MainLayout.BUTTON_HEIGHT);
    btnOk.addSelectionListener(new SelectionListener<ButtonEvent>() {
      @Override
      public void componentSelected(ButtonEvent be) {
        if(form.isValid()){
          upload();
        }
        else{
          MessageBox.info("Validation Error", "You must enter all required fields.", null);
        }
      }
    });
//    buttonBar.add(btnOk);
    form.add(btnOk);

    btnCancel = new Button(cancel);
    btnCancel.setSize(MainLayout.BUTTON_WIDTH, MainLayout.BUTTON_HEIGHT);
    btnCancel.addSelectionListener(new SelectionListener<ButtonEvent>() {
      @Override
      public void componentSelected(ButtonEvent be) {
        exit();
      }
    });
//    buttonBar.add(btnCancel);
    form.add(btnCancel);
    }

  /**
   * 
   */
  private void buildUploadForm() {
    form.setFrame(true);  
    form.setEncoding(Encoding.MULTIPART);  
    form.setMethod(Method.POST);  
    form.setButtonAlign(HorizontalAlignment.CENTER);  
    form.setWidth(MainLayout.CONTENT_WIDTH);  
    form.addListener(Events.Submit, new Listener<FormEvent>() {
      public void handleEvent(FormEvent fe) {
        System.out.println(fe.getResultHtml());
      };
    });  

    String desc = StyleHelper.formatPrompt(description);
    textDescription = new TextField<String>();
    textDescription.setAllowBlank(false);
    textDescription.setFieldLabel(desc);
    form.add(textDescription);

    String fileLabel = StyleHelper.formatPrompt(file);

    uploadFile = new FileUploadField();
    uploadFile.setAllowBlank(false);
    uploadFile.setFieldLabel(fileLabel);
    form.add(uploadFile);
  }

  /**
   * Jump back to the landing page
   */
  protected void exit(){
    ImageViewer.getImageViewer().showLanding();
  }

  /**
   * 
   */
  protected void upload(){
    form.submit();
    exit();
  }

  /**
   * Load the URL from the person service
   */
  private void loadUrl() {
    personViewService.buildUploadUrl(new AsyncCallback<String>() {

      @Override
      public void onFailure(Throwable caught) {
        System.err.println("Unable to construct URL");
        caught.printStackTrace();
        MainLayout.showError(caught.getMessage());
      }

      @Override
      public void onSuccess(String url) {
        form.setAction(url);
      }
    });
  }

  /**
   * GWT-Designer will change to return true during design time
   * 
   * @return
   */
  private static final boolean isDesignTime() {
    return false;
  }
}

如您所见,URL是动态构建的。

问题是当我运行它并单击OK按钮时,servlet永远不会被调用。如果我查看呈现的HTML并复制操作中的URL,然后将其粘贴到我的浏览器中,它会调用servlet,因此URL正常。

我还确认正在调用提交。

有人能指出我正确的方向吗?我正在关注这个例子:

http://extjs-public.googlecode.com/svn/gxt/release/samples/examples/www/com.extjs.gxt.samples.Examples/forms/fileupload.html

2 个答案:

答案 0 :(得分:0)

尝试这个(或调试它)并查看是否曾调用过上传。也许你的表格无效:

//if(form.isValid()){
          upload();
  //      }

答案 1 :(得分:0)

我认为问题是由于我在提交内容完成之前隐藏表单引起的。很难说,因为我从一个我认识的例子开始,这是一个对话,所以我重新开始并一次改变一两件事并进行测试。

因此,更改为对话框可能是其中的一部分,但如果我在提交FormPanel后立即隐藏表单,它肯定无效。我添加以下代码等待四分之一秒,它解决了问题。

    Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
      @Override
      public boolean execute() {
        hide(selectedButton);
        return false;
      }
    }, 250);