1

我正在使用 GWT,我需要在具有设置宽度和高度的新窗口中打开表单提交的结果。我可以让它在新选项卡中打开,但不知道如何让它在新窗口中加载,也不知道如何设置宽度和高度。这是我的代码:

 @UiHandler({"myGWTButton"})
    protected void myGWTButtonClicked(ClickEvent click) {
      click.preventDefault();
      FormPanel form = new FormPanel("_blank");
      form.setAction("www.mydomain.com");
      form.setMethod(FormPanel.METHOD_POST);
      form.setEncoding("multipart/form-data");
      TextArea params0 = new TextArea();
      params0.setName("foo");
      params0.setValue("bar");
      form.setVisible(false);
      form.add(params0);
      RootPanel.get().add(form);
      form.submit();
    }
4

3 回答 3

3

尝试使用 GWT 似乎是我想太多了。我依靠本机javascript解决了我的问题。

 @UiHandler({"myGWTButton"})
    protected void myGWTButtonClicked(ClickEvent click) {
    click.preventDefault();
    openPopupWindow("www.mydomain.com", 'bar');
}

native void openPopupWindow(String url, String val) /*-{
       var windowName = '' + Math.floor(Math.random() * 10000000) ; //we need some name to reference it later, so assign a random number as the name (make sure the number doesn't have a decimal because IE will choke on it)
       var popupWindow =  window.open('', windowName, 'width=550,height=350,resizeable,scrollbars');
       var form = document.createElement("form");
       form.setAttribute("method", "post");
       form.setAttribute("action", url);
       form.setAttribute("target", windowName);
       form.setAttribute("enctype", "multipart/form-data");
       var hiddenField = document.createElement("input");
       hiddenField.setAttribute("type", "hidden");
       hiddenField.setAttribute("name", "foo");
       hiddenField.setAttribute("value", val);
       form.appendChild(hiddenField);
       document.getElementsByTagName('body')[0].appendChild(form);
       while(!popupWindow){
         //wait for popupwindow to open before submitting the form
       }
       form.submit();

    }-*/;
于 2013-08-15T20:25:10.277 回答
0

表单似乎在单独的 iframe 中获得结果。问题是,由于跨站点限制 (SOP),您无法真正与此 iframe 进行交互。所以我不确定有没有办法做到这一点。

你能不改用 HTTP 请求吗?

于 2013-08-15T02:46:14.863 回答
0

如果您可以使用 GET 方法而不是 POST,我可以为您提供解决方案。

@UiHandler({"myGWTButton"})
protected void myGWTButtonClicked(ClickEvent click) {
  click.preventDefault();

  Window.open(new UrlBuilder()
        .setHost("www.mydomain.com")
        .setParameter("foo", "bar")
        .buildString(), "_blank", "width=200,height=200");
}
于 2013-08-15T12:17:37.423 回答