【问题标题】:how do i get gwt form submit to open in a new window with set dimensions?如何让 gwt 表单提交以在设置尺寸的新窗口中打开?
【发布时间】:2013-08-14 22:19:05
【问题描述】:

我正在使用 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();
    }

【问题讨论】:

    标签: gwt resize submit new-window


    【解决方案1】:

    我似乎在尝试使用 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();
    
        }-*/;
    

    【讨论】:

      【解决方案2】:

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

      你能不改用 HTTP 请求吗?

      【讨论】:

        【解决方案3】:

        如果您可以使用 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");
        }
        

        【讨论】:

        • 感谢您的回复。由于我要发送的数据的大小,我无法使用 GET。
        • @Boundless 您是否提交到与原始页面相同的域:端口?如果否,您提交的服务器是否允许跨域请求?因为如果您对其中一些问题的回答是肯定的,您可以提交到隐藏框架之后,您可以从原始页面访问框架结果并打开新窗口,将结果从框架传递到窗口。
        • 我确实提交到同一个域。我最终使用本机 javascript 解决了这个问题。如果您想查看,我发布了解决方案。
        猜你喜欢
        • 2014-05-03
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        • 2012-12-02
        • 1970-01-01
        相关资源
        最近更新 更多