关闭由Window.open()创建的窗口类中的选项卡,该窗口类源自GWT中的JavaScriptObject

问题描述 投票:0回答:1

我需要使用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);
       ...
    }

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

javascript java gwt
1个回答
0
投票

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

Window.Location.assign(__file_servlet_path__);

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

resp.setHeader("Content-disposition", "attachment; filename=\"" + __file_name__ + "\"");
© www.soinside.com 2019 - 2024. All rights reserved.