GWT Java - 如何关闭 window(注销)

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

我读到要注销应用程序需要关闭 window,我发现了这段代码:

这个答案有你要找的: How to run JavaScript function from GWT Java with JSNI?

具体在Java:

myButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
closeWindow();
};
});

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

然后在您应用的 .html 页面的Java脚本中:

<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(); 

"The method closeWindow() is undefined for the type new ClickHandler(){}"

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

此行有多个标记 - 语法错误,插入 "EnumBody" 以完成 BlockStatement - 令牌语法错误 "void",@ expected - 语法错误,插入 "enum Identifier" 完成 枚举头名称

感谢所有回复的人。根据您的回答... 我在我的应用程序中使用类似(通过 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 
}

这似乎有效。但是,我无法在本地测试多个会话,而且我没有可以部署到的测试服务器。所以我可以请知道他们在这个 space 中做什么的人检查它以确保我不会将问题引入生产中。我最担心的是这会使所有人注销。我特别敏感,因为我遇到过会话没有划分的情况,用户可以看到其他人的数据。这已得到修复,我不想破坏该修复!!

  1. 如果 window 已被用户打开,则无法使用 JavaScript 关闭 window。您只能关闭由您的应用程序打开的新 window。

  2. 关闭 window 将不会影响用户身份验证,因为大多数身份验证机制都依赖于服务器会话或 cookie。

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

Javascript 只能关闭同一个脚本打开的页面。所以 closeWindow() 甚至不会工作。所以:

  1. 如果您没有在您的应用程序中使用会话,即您认为只关闭 window 是一个可以实现的目标。然后只需从 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(); returns 我登录 window。 Window.Location.assign("/"); returns 我到 tomcat 页面。 适合自己的就用吧。