Window.open()中的Window.open()创建的关闭选项卡从GWT中的JavaScriptObject派生的

时间:2017-12-19 12:40:20

标签: javascript java gwt

我需要使用GWT关闭由JavaScriptObject派生的类创建的“tab”。 这是一段代码: -

public class MyWindow extends JavaScriptObject {
    // All types that extend JavaScriptObject must have a protected,
    // no-args constructor. 
    protected MyWindow() {}

    public static native MyWindow open(String url, String target, String options) /*-{
      return $wnd.open(url, target, options);
    }-*/;

    public static final native void close() /*-{
      this.close();
    }-*/;

    public static final native void setUrl(String url) /*-{
      if (this.location) {
        this.location = url;
      }
    }-*/;
}

public class MyUsingClass {
    // Have to have my own window object derived from JavaScriptObject.
    // Cannot use standard Window, because we are in an async callback,
    // and browser thinks it's a popup!
    MyWindow myWindow = MyWindow.open(null, "_blank", null);
    ...
    @Override
    public void registeredCallback(ArrayList<String> params)
    {
       ...
       // The URL passed to setUrl, is a call to a servlet that returns a 
       // response with Content-Disposition set in the header.
       // We get a File SaveAs Dialog box displayed, 'owned', I assume, by
       // this.myWindow.
       myWindow.setUrl(url);
       ...
    }

我遇到的问题是,对话框按预期运行,并在按下确定或取消按钮时关闭,但是,我无法弄清楚如何[以及何时/何时]关闭通话所创建的选项卡到MyWindow.open()。 我猜我不知何故需要捕获由File SaveAs Dialog关闭触发的事件? 任何帮助将非常感激。 谢谢!

1 个答案:

答案 0 :(得分:0)

您不必打开新标签(窗口)只是为了保存文件对话框。我使用当前窗口(当我确定文件可以访问时)我只是这样做:

Window.Location.assign(__file_servlet_path__);

在servlet中,我设置了Content-typeContent-disposition标题:

resp.setHeader("Content-disposition", "attachment; filename=\"" + __file_name__ + "\"");