jsfwindow.onunload

Page unload event handling in JSF


Is there any way to handle page unLoad event in JSF 2.0? I want to perform some data reset whenever the user move away from a particular page?


Solution

  • This is not supported by standard JSF. Your best bet is using OmniFaces @ViewScoped. Then you can just do your job in a method annotated with @PreDestroy.

    import jakarta.inject.Named;
    import org.omnifaces.cdi.ViewScoped;
    
    @Named
    @ViewScoped
    public class Bean implements Serializable {
    
        @PreDestroy
        public void destroy() {
            // ...
        }
    }