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

Javascript GWT Java-如何关闭窗口(注销),javascript,java,gwt,Javascript,Java,Gwt,我读到要注销应用程序,需要关闭窗口,我发现以下代码: 这个答案符合您的要求: 特别是在Java中: myButton.addClickHandler(new ClickHandler(){ public void onClick(ClickEvent event) { closeWindow(); }; }); public static native void closeWindow() /*-{ $wnd.closeWindow();}-*/; 然后在应用程序的.html页面的Java

我读到要注销应用程序,需要关闭窗口,我发现以下代码:

这个答案符合您的要求:

特别是在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>
以及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>
“对于类型new ClickHandler(){},未定义closeWindow()方法”

这条线上有多个标记 -语法错误,插入“EnumBody”以完成BlockStatement -令牌“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("/");    
    }
}
        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("/");    
    }
}
这似乎奏效了。但是,我在本地测试多个会话时遇到问题,并且我没有可以部署到的测试服务器。所以,我可以请知道他们在这个领域做什么的人检查一下,以确保我没有在生产中引入问题。我最担心的是这会让所有人都退出。我特别喜欢toey,因为我遇到的情况是会话没有划分,用户可以看到其他人的数据。这已被修复,我不想破坏修复

  • 如果窗口是由用户打开的,则不能使用JavaScript关闭该窗口。您只能关闭应用程序打开的新窗口

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

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

  • 如果窗口是由用户打开的,则不能使用JavaScript关闭该窗口。您只能关闭应用程序打开的新窗口

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


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

    Javascript只能在使用相同脚本打开页面时关闭页面。所以closeWindow()甚至不起作用。因此:

  • 如果您没有在应用程序中使用会话,即您认为只有关闭窗口才是要实现的目标。然后简单地从DOM中删除该iframe,而不是关闭页面。(您可以使用js来实现这一点。)
  • document.getElementById('iframeid')。innerHTML=''

  • 如果您在应用程序中使用类似(通过RemoteServiceServlet)的会话,则需要先使会话无效,然后从dom中删除元素。(对此,我不知道该怎么办。)
  • 您可以只重新加载iframe(这被视为重新加载您的应用程序),而不是删除:

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


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

  • 如果您没有在应用程序中使用会话,即您认为只有关闭窗口才是要实现的目标。然后简单地从DOM中删除该iframe,而不是关闭页面。(您可以使用js来实现这一点。)
  • document.getElementById('iframeid')。innerHTML=''

  • 如果您在应用程序中使用类似(通过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("/");    
        }
    }
    
            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("/");    
        }
    }
    
    getThreadLocalRequest().getSession().invalidate();返回我的登录窗口。 Window.Location.assign(“/”);返回到tomcat页面。
    因此,请使用适合您的代码。

    这是我最后使用的代码: 我正在应用程序中使用会话(通过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("/");    
        }
    }
    
            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("/");    
        }
    }
    
    getThreadLocalRequest().getSession().invalidate();返回我的登录窗口。 Window.Location.assign(“/”);返回到tomcat页面。
    所以,选择适合你的。

    这与这个问题有什么关系?我知道这是一个不同的观点。但它可能会有所帮助,因为目标是执行JavaScript的closeWindow()方法&绑定是在java中完成的,即使用click处理程序。但是如果绑定是使用Jquery或Js完成的,比如Jquery(document).ready(function(){$(“#frameDemo”).contents().find(“#closeBtn”).on('click',function(){window.open('','u self','');window.close()});首先,OP使用的是GWT,而不是JQuery。第二,JavaScript不允许关闭当前脚本未打开的窗口。这与问题有什么关系?我知道这是不同的观点。但它可能会有所帮助,因为目标是执行JavaScript的closeWindow()方法&绑定是在java中完成的,即使用click处理程序。但是如果绑定是使用Jquery或Js完成的,比如Jquery(document).ready(function(){$(“#frameDemo”).contents().find(“#closeBtn”).on('click',function(){window.open('','u self','');window.close();}
    public void invalidateSession() {
        getThreadLocalRequest().getSession().invalidate(); // kill session 
    }