0

In my project, many web pages are there and each one having own URL like

localhost:8080/MyProject/login
localhost:8080/MyProject/logout
localhost:8080/MyProject/denied
localhost:8080/MyProject/home 

when my project run showing as localhost:8080/MyProject/login. when shows this type of url in browser tab I don't want any method calling. when tab or browser closing wants to call a custom method using spring can any one help me how to call a method when tab or browser closing.

Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31

1 Answers1

1
  1. You have to add the method you want to invoke to your controller and properly map the URL to it.
  2. There is additional JS is necessary to invoke this method on the server. The thing you're interested in is called: onbeforeunload

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

You can bind your action as it's written on the document above, or you can use jQuery and do it like this:

$(window).bind('beforeunload', function(eventObject) {
  ...
}); 
  1. Inside this callback you need to send a request to your controller and prevent the default logic if necessary. You can use ajax(Asynchronous JavaScript And XML).
dvelopp
  • 4,095
  • 3
  • 31
  • 57