gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:18:15 25 4
gpt4 key购买 nike

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

这个答案有您想要的内容: 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 页面中使用 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(); 

“未针对 new ClickHandler(){} 类型定义 closeWindow() 方法”

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

该行有多个标记 - 语法错误,插入“EnumBody”来完成BlockStatement - 标记“void”上的语法错误,@预期 - 语法错误,插入“enum Identifier”完成 枚举标题名称

感谢所有回复的人。根据您的回复...我在我的应用程序中使用类似(通过 RemoteServiceServlet)的 session 。因此,根据下面的响应,我需要首先使 session 无效,然后从 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
}

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

最佳答案

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

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

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

关于javascript - GWT Java - 如何关闭窗口(注销),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35737826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com