GWT Java - 如何关闭窗口(注销)

GWT Java - How to close the window (Log Out)

本文关键字:注销 窗口 何关闭 Java GWT      更新时间:2023-09-26

我已经读到要注销应用程序,您需要关闭窗口,我找到了以下代码:

这个答案有你要找的:如何使用JSNI从GWT Java运行JavaScript函数?

特别是在Java中:

myButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
closeWindow();
};
});
public static native void closeWindow() /*-{ $wnd.closeWindow();}-*/;

然后在应用的 .html 页面中的 JavaScript 中:

<script type="text/javascript" language="javascript">
function closeWindow() {
window.open('','_self','');
window.close();
}</script>

我已经在我的应用程序中通过以下方式实现了这一点:

    //Log Out Button
    Button logOutButton = new Button("Log Out");
    logOutButton.addClickHandler(new ClickHandler(){
        public void onClick(ClickEvent event) {
        closeWindow();
        }
    });
    public static native void closeWindow() /*-{ $wnd.closeWindow();}-*/;

和 HTML:

<!doctype html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <!--                                                               -->
        <!-- Consider inlining CSS to reduce the number of requested files -->
        <!--                                                               -->
        <!-- <link type="text/css" rel="stylesheet" href="org.AwardTracker.AwardTracker.AwardTracker.css"> -->
        <!--                                           -->
        <!-- Any title is fine                         -->
        <!--                                           -->
        <title>Wrapper HTML for AwardTracker</title>
        <!--                                           -->
        <!-- This script loads your compiled module.   -->
        <!-- If you add any GWT meta tags, they must   -->
        <!-- be added before this line.                -->
        <!--                                           -->
        <!-- script language="javascript" src="org.AwardTracker.AwardTracker/org.AwardTracker.AwardTracker.nocache.js" --><!-- /script -->
        <script src="org.AwardTracker.AwardTracker/org.AwardTracker.AwardTracker.nocache.js">
            <type="text/javascript">
            function closeWindow() {
                window.open('','_self','');
                window.close();
            }
        </script>
    </head>
    <!--                                           -->
    <!-- The body can have arbitrary html, or      -->
    <!-- we leave the body empty because we want   -->
    <!-- to create a completely dynamic ui         -->
    <!--                                           -->
    <body>
        <!-- OPTIONAL: include this if you want history support -->
        <iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>
    </body>
</html>

但是,我在行上收到以下错误:

closeWindow(); 

"方法 closeWindow() 未定义类型为 new ClickHandler(){}"

public static native void closeWindow() /*-{ $wnd.closeWindow();}-*/;

此行处有多个标记 - 语法错误,插入"EnumBody"以完成阻止语句 - 令牌"void"上的语法错误,@ 预期 - 语法错误,插入"枚举标识符"即可完成 枚举标头名称

感谢所有回复的人。根据您的回答...我在我的应用程序中使用类似(通过RemoteServiceServlet)的会话。因此,按照下面的响应,我需要首先使会话无效,然后从 dom 中删除元素。所以尝试了以下方法:

在客户端:

        logOutButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            //Invalidate the session and then reload the application.
            AsyncCallback<Void> callback = new InvalidateSessionHandler<Void>(SelectPersonView.this);
            rpc.invalidateSession(callback);
            }
        });
class InvalidateSessionHandler<T> implements AsyncCallback<Void> {
    SelectPersonView view;
    public InvalidateSessionHandler(SelectPersonView view) {
        this.view = view;
    }
    public void onFailure(Throwable ex) {
        System.out.println("RPC call failed - InvalidateSessionHandler - Notify Administrator.");
        Window.alert("Connection failed - please retry.");
    }
    public void onSuccess(Void result) {
        //Reload the application.
        Window.Location.assign("/");    
    }
}

在服务器端:

public void invalidateSession() {
    getThreadLocalRequest().getSession().invalidate(); // kill session 
}

这似乎有效。但是,我在本地测试多个会话时遇到问题,并且没有可以部署到的测试服务器。因此,我可以请知道他们在这个领域做什么的人来检查它,以确保我没有将问题引入生产中。我最担心的是这会让每个人都退出。我特别喜欢,因为我遇到过会话没有划分的情况,用户可以看到其他人的数据。这已修复,我不想破坏该修复!!

  1. 如果窗口是由用户打开的,则无法使用 JavaScript 关闭该窗口。您只能关闭应用打开的新窗口。

  2. 关闭窗口对用户身份验证没有影响,因为大多数身份验证机制依赖于服务器会话或 Cookie。

如果身份验证是基于会话的,则当用户单击"注销"按钮时,您需要 (1) 使用户的会话无效,以及 (2) 重新加载应用,这将显示未经身份验证的用户的默认入口点(主页或登录页面)。

Javascript 只有在用同一脚本打开页面时才能关闭页面。所以closeWindow()甚至不起作用。所以:

  1. 如果您没有在应用程序中使用会话,即您认为只有关闭窗口是要实现的目标。然后只需从 DOM 中删除该 iframe 而不是关闭页面。(你可以通过使用js来做到这一点。

document.getElementById('iframeid').innerHTML = '';

  1. 如果您在应用程序中使用类似(通过 RemoteServiceServlet)的会话,那么您需要先使会话无效,然后从 dom 中删除元素。 (为此,我不确定该怎么做。

您可以重新加载

iframe(这被视为重新加载您的应用),而不是删除:

document.getElementById('iframeid').src = document.getElementById('iframeid').src

这是我

使用的最终代码:我正在我的应用程序中使用会话(通过RemoteServiceServlet)。因此,我需要首先使会话无效,然后从 dom 中删除元素。所以以下是最终代码:

在客户端:

        logOutButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            //Invalidate the session and then reload the application.
            AsyncCallback<Void> callback = new InvalidateSessionHandler<Void>(SelectPersonView.this);
            rpc.invalidateSession(callback);
            }
        });
class InvalidateSessionHandler<T> implements AsyncCallback<Void> {
    SelectPersonView view;
    public InvalidateSessionHandler(SelectPersonView view) {
        this.view = view;
    }
    public void onFailure(Throwable ex) {
        System.out.println("RPC call failed - InvalidateSessionHandler - Notify Administrator.");
        Window.alert("Connection failed - please retry.");
    }
    public void onSuccess(Void result) {
        //Reload the application.
        Window.Location.assign("/");    
    }
}

在服务器端:

public void invalidateSession() {
    getThreadLocalRequest().getSession().invalidate(); // kill session 
}

getThreadLocalRequest().getSession().invalidate(); 将我返回到我的登录窗口。Window.Location.assign("/");让我返回到雄猫页面。因此,请使用适合您的方法。