1

当我在打开应用程序的情况下关闭浏览器窗口时,我也想结束 http 会话。我已经尝试在 GWT 中关闭窗口时执行代码,但我在 onClose 方法中放置的任何内容都不会运行。

Window.addCloseHandler(new CloseHandler<Window>()
    {

        @Override
        public void onClose(CloseEvent<Window> event)
        {
            //Code in here never executes!
            alert("Hi");
            Window.Location.replace(GWT.getHostPageBaseURL() + "logout.jsp");
        }
    });
4

1 回答 1

0

There are actually two methods you can use:

  • Window.addWindowClosingHandler: Registers for ClosingEvent, which is fired by JavaScript's $wnd.onbeforeunload.
  • Window.addCloseHandler: Registers for CloseEvent, which is fired by JavaScript's $wnd.onunload.

see WindowImpl.initWindowCloseHandler(), and also WindowImplIE

The ClosingEvent is fired a little bit earlier than the CloseEvent.

(There's a third method, Window.addWindowCloseListener, which effectively registers both for $wnd.onbeforeunload and $wnd.onunload, but that method is deprecated.)

The reason why you don't see the alert is probably explained with the javadoc of ClosingHandler.onWindowClosing():

 * Fired just before the browser window closes or navigates to a different
 * site. No user-interface may be displayed during shutdown.

And instead of Window.location.replace() you should rather make an ajax call for the logout (e.g. using GWT-RPC, or RequestBuilder). The user closes the window, so it can't replace its location anymore.

于 2012-10-16T11:08:44.680 回答