在GWT中确认浏览器退出 [英] Confirm Browser Exit in GWT

查看:118
本文介绍了在GWT中确认浏览器退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户试图离开我的GWT应用程序时显示一个确认对话框。如果用户选择留在应用程序中,则不做任何事情,否则应用程序应在关闭前首先向服务器发送注销请求。



我该怎么做?

使用哪个处理程序?


Window.addCloseHandler


Window.addWindowClosingHandler


解决方案

它的方法 addWindowClosingHandler 似乎是你需要的。



编辑:我认为你需要做的是... 当窗口关闭时,即当用户点击关闭或重新加载按钮时,调用addWindowClosingHandler 。当窗口关闭时调用 addCloseHandler 。所以你使用 !您使用结束处理程序来显示确认对话框,然后使用关闭处理程序来完成您只想关闭时执行的操作。

  Window.addWindowClosingHandler(new Window.ClosingHandler(){
@Override
public void onWindowClosing(ClosingEvent event){
event.setMessage(Do you want to close?);
System.out.println(Closing ...);
}
});

Window.addCloseHandler(new CloseHandler< Window>(){
@Override
public void onClose(CloseEvent< Window> event){
System.out.println (Closed!);
}
});


I want to display a confirmation dialog when users tries to leave my GWT application. If the user chooses to stay in the app, nothing is done otherwise the application should first send a logout request to the server before closing.

How can I do that?

which handler to use?

Window.addCloseHandler

or

Window.addWindowClosingHandler

解决方案

Based on the methods it has, addWindowClosingHandler seems to be what you need.

EDIT: I think what you need to do is... addWindowClosingHandler is called when the window is closing, i.e. when the user clicks the close or reload button. addCloseHandler is called when the Window closes. So you use both! You use the closing handler to display a confirmation dialog, then you use the close handler to do the stuff you only want to do on close.

    Window.addWindowClosingHandler(new Window.ClosingHandler() {
        @Override
        public void onWindowClosing(ClosingEvent event) {
            event.setMessage("Do you wanna close?");
            System.out.println("Closing...");
        }
    });

    Window.addCloseHandler(new CloseHandler<Window>() {
        @Override
        public void onClose(CloseEvent<Window> event) {
            System.out.println("Closed!");
        }       
    });

这篇关于在GWT中确认浏览器退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆