问题
I have google'd quite a lot but was not able to find a solution for this problem. I have a Vaadin application that runs in a browser window. I have a logout button on it, clicking on it must invalidate the session and close the browser window. I was able to get this code to invalidate the session and close the application. However, I am looking to close the browser window also, which is where I am not having any success
WebApplicationContext webCtx = (WebApplicationContext) appRef.getMainWindow().getApplication().getContext();
HttpSession session = webCtx.getHttpSession();
session.invalidate();
appRef.getMainWindow().getApplication().close();
I am using vaadin 6.x and tried the following but they don't work on browsers I tried which is Chrome and IE.
appRef.getMainWindow().executeJavaScript("window.close();");
Any ideas would be greatly appreciated. Another question I have is do I need to get the name of the main Window and then call mainWindow.close(); or just window.close() ?
回答1:
- Read https://vaadin.com/book/vaadin6/-/page/application.close.html if you haven't done so far.
- If appRef is already your Application, you do not have to call
appRef.getMainWindow().getApplication()
to get the Application. Just doappRef.close();
- Do not invalidate the session manually. It messes with the vaadin lifecycle, so the next lines are not executed anymore, at least not in the vaadin context. Just do "application.close()" and let vaadin do the rest.
- In vaadin 6 "window.close()" works, i use it with IE and chrome. So after you removed the session invalidation stuff, your code
appRef.getMainWindow().executeJavascript("window.close()");
will work as expected.
来源:https://stackoverflow.com/questions/17839601/close-the-browser-window-in-vaadin